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?
# 1  
Old 05-05-2011
sed or awk - removing part of line?

hi all,

I am having trouble finding the right string for this - I dont know whether to use awk or sed..

If I have a file with alot of names and phone numbers like this
Code:
McGowan,Sean  978-934-4000
 Kilcoyne,Kathleen 603-555-1212
 Club603,The 617-505-1332
 Boyle,William 301-444-1221

And I want to print just the line with the 603 area code plus remove all area codes int he final output sorted alpha by last name - how would I do it?

would I need to define a character set?
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-05-2011 at 04:45 PM.. Reason: code tags, please!
# 2  
Old 05-05-2011
Code:
nawk '$2~/^603-/{$2=substr($2,5);print}' myFile |sort

# 3  
Old 05-05-2011
is there a way to do this using sed?
# 4  
Old 05-05-2011
Code:
sed -e 's/ [0-9]*-/ /' myFile

---------- Post updated at 10:43 PM ---------- Previous update was at 10:41 PM ----------

Oooops : no need for the -e :

Code:
sed 's/ [0-9]*-/ /' infile

# 5  
Old 05-05-2011
Code:
sed -n '/ 603-/s/ 603-/ /p' myFile | sort

# 6  
Old 05-05-2011
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?
# 7  
Old 05-05-2011
Ooops yep i forgot that 603 ...
similar one
Code:
sed '/ 603-/!d;s/ 603-/ /' infile | sort

 
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