sed or awk - removing part of line?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed or awk - removing part of line?
# 8  
Old 05-05-2011
Quote:
Originally Posted by vgersh99
Code:
sed -n '/ 603-/s/ 603-/ /p' myFile | sort


this works thanks!
# 9  
Old 05-05-2011
Quote:
Originally Posted by alis
This works except that the column with the area code is $3 not $2 so when I substituted $2 for $3 - the command works !!
Trying to understand this tho...I know $3=substr means substitution in column 3 but where did the 5 come from? please explain?
How many characters are in 603-?
# 10  
Old 05-05-2011
Quote:
Originally Posted by vgersh99
How many characters are in 603-?
I see 4..counting a space 5 ..why would you count the space..there is no space in the original file after -
Sorry I am a newbie.
# 11  
Old 05-05-2011
Quote:
Originally Posted by alis
I see 4..counting a space 5 ..why would you count the space..there is no space in the original file after -
Sorry I am a newbie.
Quoting the strings in the forums can get confusing - let me try it again.
How many characters do you count in quoted string ' 603-' ?

There's a space AFTER the first name and the AREA CODE.
You want to remove the area code. How would you distinguish an area code '603' from the '603' appearing in the rest of the phone number?

Or alternatively:
Code:
nawk '$2~/^603-/&& sub("603-","",$2)' myFile | sort


Last edited by vgersh99; 05-05-2011 at 06:33 PM..
# 12  
Old 05-06-2011
@vgersh99

Here is a caveat for you and for those readers that are not already aware of it (you can give it a try) :

your statement
Code:
# sed -n '/ 603-/s/ 603-/ /p' infile

can be written
Code:
# sed -n '/ 603-/s// /p' infile

because the empty // will then refer to the previous match Smilie

You will notice that the following does NOT work ... contrary to what we could have thought:
Code:
# sed -n '/ 603-/s/&/ /p' infile


Last edited by ctsgnb; 05-06-2011 at 05:52 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command for picking a part of line

Hello Friends I want to use sed command to pick a part of line. FOr example I only need the /home_put1/bidds/myfo part of following line fish://ulavet@rits1.ula.com.tr/home_put1/bidds/myfo How can I do this bu using sed command ? (2 Replies)
Discussion started by: rpf
2 Replies

2. Shell Programming and Scripting

sed removing until end of line

All: Can somebody help me out with a sed command, which removes the the first occurance of ')' until the end of the line If I have the following input ... (5 Replies)
Discussion started by: BeefStu
5 Replies

3. UNIX for Dummies Questions & Answers

Removing end of line using SED

Hello Friends, How can I remove the last two values of this line using sed John Carey:507-699-5368:29 Albert way, Edmonton, AL 25638:9/3/90:45900 The result should look like this: John Carey:507-699-5368:29 Albert way, Edmonton, AL 25638 (3 Replies)
Discussion started by: humkhn
3 Replies

4. Shell Programming and Scripting

Using Sed to remove part of line with regex

Greetings everyone. Right now I am working on a script to be used during automated deployment of servers. What I have to do is remove localhost.localdomain and localhost6.localdomain6 from the /etc/hosts file. Simple, right? Except most of the examples I've found using sed want to delete the entire... (4 Replies)
Discussion started by: msarro
4 Replies

5. Shell Programming and Scripting

change part of a line with sed

Hi gurus, I'd like to change this complete line on a file: BAN_COMMAND="/etc/apf/apf -d $ATTACK_HOST {bfd.$MOD}" to this one: BAN_COMMAND="/sbin/iptables -A INPUT -s $ATTACK_HOST -j DROP" I've tried a lot without any successful . :( thanks in advance Israel. (2 Replies)
Discussion started by: iga3725
2 Replies

6. Shell Programming and Scripting

Sed question (Removing a line of text)

I am working with bash on HP-UX server at school. As practice for scripting, I am trying to make a pretend server admin script that adds a user to the system, deletes a user from the system, and lists all users of the pretend system. I have accomplished this with a select loop. Adding users, and... (2 Replies)
Discussion started by: masterscout1977
2 Replies

7. Shell Programming and Scripting

Replace part of a line with sed/awk

Hello I have a document and in this document I have several occurrence of "VAR == xxxxxxx" and xxxxx can be anything. I don't know what it is. I want to replace the 'xxxxx's with something I know. What I know however, is the line numbers of the VAR =='s in the file. How can I replace... (1 Reply)
Discussion started by: alirezan
1 Replies

8. Shell Programming and Scripting

Getting sed to work on part of a line

I been trying to get this right. I have trying to get rid of spaces in between the character < and the character >. Everytime I try, sed gets too greedy and do the whole line. Ex. < T AG 1> Hello, how are you doing? <Tag 2> I am doing fine. I want this: <TAG1> Hello, how are you... (6 Replies)
Discussion started by: quixoticking11
6 Replies

9. Shell Programming and Scripting

removing a line containing a pattern in sed

i need to use sed to remove an entire line containing a pattern stored in a variable say $var1 this var1 will be a URL and will therefore contain slashes any help would be greatly appreciated (1 Reply)
Discussion started by: Fire_Storm
1 Replies

10. UNIX for Dummies Questions & Answers

removing parts of a line with SED

hi, i'm trying to erase all the characters after, and including, the first test test Output: test1 test2 test3 this is what I tried, but didn't work sed "s/*//" file > testfilename any suggestions? thanks, gammmaman (2 Replies)
Discussion started by: gammaman
2 Replies
Login or Register to Ask a Question