How to delete a character using sed and or awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete a character using sed and or awk?
# 1  
Old 07-28-2014
How to delete a character using sed and or awk?

Hi,
1/
i have file test.txt
Code:
1 Jul 28 08:35:29 2014-07-28 Root::UserA
1 Jul 28 08:36:44 2014-07-28 Root::UserB

i want to delete the seconds of the file, and the Root:: and the output will be:
Code:
1 Jul 28 08:35 2014-07-28 UserA 
1 Jul 28 08:36 2014-07-28 UserB

2/i have another file test2.txt:
Code:
Root Test Jul 28 08:35 2014-07-28 UserA 1.2.3.4 4.3.2.1
Root Test Jul 28 08:36 2014-07-28 UserB 2.5.2.4 5.4.2.2

x.x.x.x is an IP address

i want compare the 2 files and create a new file with the result
Code:
1 Jul 28 08:35 2014-07-28 UserA  1.2.3.4 4.3.2.1
1 Jul 28 08:36 2014-07-28 UserB   2.5.2.4 5.4.2.2

Any help would be appreciated

Last edited by fxsme; 07-28-2014 at 11:53 AM.. Reason: code tags
# 2  
Old 07-28-2014
1)
Code:
sed 's/:[0-9][0-9]//2' test.txt

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 07-28-2014
Hi,

For 1/
Code:
sed -e s/:..[^:]/\ /-e  s/Root:[$:]/\ / test.txt


Last edited by fxsme; 07-28-2014 at 11:54 AM..
# 4  
Old 07-28-2014
Please use code tags as required by forum rules!

To solve problem 2: How would you connect the users to the respective IP addresses?
# 5  
Old 07-28-2014
Hi

I've updated the file test2.txt.Smilie
# 6  
Old 07-28-2014
Try
Code:
awk 'NR==FNR {T[$7]=$8" "$9; next} {gsub (/:.. |Root::/, " "); print $0, T[$6]}' file2 file1
1 Jul 28 08:35 2014-07-28  UserA 1.2.3.4 4.3.2.1
1 Jul 28 08:36 2014-07-28  UserB 2.5.2.4 5.4.2.2

If you can't accept two spaces in front of the UserX, try
Code:
awk 'NR==FNR {T[$7]=$8" "$9; next} {sub (/:..$/, "", $4); sub (/Root::/, "", $6); print $0, T[$6]}' file2 file1

# 7  
Old 07-29-2014
A new problem originally tacked onto this thread has been moved to: Find matching timestamps in two files.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

(g)awk conditional substitution issues when attempting to delete character

A portion of my input is as follows: 1087 IKON01,49 A WA- -1 . -1 . 0 W WA- -1 . -1 . 0 . -1 . -1 -1 -1 -1 -1 -1 W 1088 IKON01,49 A J.@QU80MW. 2... (2 Replies)
Discussion started by: jvoot
2 Replies

2. Shell Programming and Scripting

Want to remove / and character using awk or sed

Below i am trying to remove "/" and "r" from the output, so i need output as: hdiskpower3 hdisk0 hdisk1 #inq | grep 5773 | awk '{print $1}' | sed 's/dev//g' | awk -F"/" '{$1=$1}1' .....................................................//rhdiskpower0 //rhdiskpower1 //rhdiskpower2... (3 Replies)
Discussion started by: aix_admin_007
3 Replies

3. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

4. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

5. Shell Programming and Scripting

KSH: substitution character, AWK or SED?

Hi Gurus, I am working with a korn shell script. I should replace in a very great file the character ";" with a space. Example: 2750;~ 2734;~ 2778;~ 2751;~ 2751;~ 2752;~ what the fastest method is? Sed? Awk? Speed is dead main point, Seen the dimensions of the files Thanks (6 Replies)
Discussion started by: GERMANICO
6 Replies

6. Shell Programming and Scripting

Delete character in determinate position with sed/awk

Hello. I'm trying to delete one character in determinate position. Example: qwEtsdf123Ecv34 <delete character in positión 3> Result: qwtsdf123Ecv34 Plase, help me. Thanks (4 Replies)
Discussion started by: maria_florencia
4 Replies

7. Shell Programming and Scripting

sed to delete character 0 only when it's on its own?

Hi all I am trying to get my head around doing the following.... I have an input field that could contain either a number a blank field or a whitespace field. What I want to do is delete a 0 (zero) if it's on its own or leading the number. So:- \t0 delete the zero 0 delete the... (8 Replies)
Discussion started by: Bashingaway
8 Replies

8. Shell Programming and Scripting

Special Character SED/AWK removal

I have a script that produces an output containing '/.ssh'. I am trying to find a way of parsing only this data from a single line, without removing any other special characters contained within the output as a result of the parse. Any help would be appreciated (6 Replies)
Discussion started by: Raggedranger333
6 Replies

9. Shell Programming and Scripting

how to delete space character with sed

hi all, i want to delete a space character in word on unix script with command sed like : #dia n result: #dian is there anyone will help me ? regards, cahyo (1 Reply)
Discussion started by: cahyo3074
1 Replies

10. Shell Programming and Scripting

Use sed to delete a character

I built a 12 million record file and made a mistake, one field is 1 character too long. The record is 40 bytes and ends always in 999. I am trying to delete the 37 character in each record. Is this possible without doing a cut and paste. (1 Reply)
Discussion started by: bthomas
1 Replies
Login or Register to Ask a Question