Remove area code using from awk output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Remove area code using from awk output
# 1  
Old 05-08-2013
Remove area code using from awk output

I am back again with one more question,

so I have a phonebook file with names, phone#s
for example: smith, joe 123-456-7890

using awk I want to display all entries with a specific area code. here 's what i have tried so far:

Code:
awk '$2~/^123/ {print}' phonebook

I can't figure out how to make the area code disappear and have only the last 7 digits appear as the output. Also is it possible for me to sort it by last name. I am assuming pipe sort with awk. anyone?

Thanks again

Last edited by Scott; 05-08-2013 at 09:08 PM.. Reason: Please use code tags
# 2  
Old 05-08-2013
You could try something like:
Code:
awk '
$NF ~ /^123-/ {
        $NF = substr($NF, 5)
}
1' phonebook | sort

With phonebook containing:
Code:
smith, joe 123-456-7890
jones, first middle 123-345-6789
jones, diff a/c 202-303-4004
chan, jackie 123-987-6543

the output produced is:
Code:
chan, jackie 987-6543
jones, diff a/c 202-303-4004
jones, first middle 345-6789
smith, joe 456-7890

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove carriage returns from awk output

I'm on Linux version 2.6.32-696.3.1.el6.x86_64, using the Ksh shell. I'm working with the input file: John Daggett, 341 King Road, Plymouth MA Alice Ford, 22 East Broadway, Richmond VA Orville Thomas, 11345 Oak Bridge Road, Tulsa OK Terry Kalkas, 402 Lans Road, Beaver Falls PA Eric Adams,... (2 Replies)
Discussion started by: prooney
2 Replies

2. Shell Programming and Scripting

Remove lines from output in files using awk

I have two large files (~250GB) that I am trying to remove the where GT: 0/0 or 1/1 or 2/2 for both files. I was going to use a bash with the below awk, which I think will find each line but how do I remove that line is that condition is found? Thank you :). Input 20 60055 . A ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Remove error code in output

Hi, i have the below code that will compare the value from 2 variables: ./wlst.sh JMSmon.py > out.dat w=`sed -e 's/\(.*!\)\(.*\)\(, Queue.*$\)/\2/' out.dat | awk '/'$1'/{n=0;{print $n}}'|head -n 1` if then x=`sed -e 's/\(.*!\)\(.*\)\(, Queue.*$\)/\2/' out.dat | awk '/'$1'/{n=2;next}n{print... (2 Replies)
Discussion started by: scripter123
2 Replies

4. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

5. Shell Programming and Scripting

awk remove duplicate code

Hi, In a previous, now closed thread, I found the following awk script: awk '{t=$5" "$6" "$7}END{for (i in t){print i,t}}' This code does a great job of removing duplicates by the the first four fields from a 7-field set of columns. I would very very much like to understand how this code... (3 Replies)
Discussion started by: pawelrc
3 Replies

6. Shell Programming and Scripting

awk: round output or delimit output of arithmatic string

I have a file with the following content. > cat /tmp/internetusage.txt 6709.296322 30000 2/7/2010 0.00I am using the following awk command to calculate a percentage from field 1 and 2 from the file. awk '{ print $1/$2*100 }' /tmp/internetusage.txt This outputs the value "22.3643" as a... (1 Reply)
Discussion started by: jelloir
1 Replies

7. Shell Programming and Scripting

SED remove line feed and add to certain area

Hi All, I have a xml file and requirement is to remove the line feed and add line feed after some element. <?xml version="1.0" ?> <AUDITRECORDS> <CARF> <HED> <VN1>20090616010622</VN1> <VN2>0</VN2> <VN3>1090</VN3> <VN4>CONFIG_DATA</VN4> ... (8 Replies)
Discussion started by: sreejitnair123
8 Replies

8. Shell Programming and Scripting

perl array matching between area code and no.

Hi Everyone, I have: my @No=qw(032106 032630 0380 034010 035110 0354801111); my $str_No=join(';', @No); I have a string $strA="03263033", so in order to determine this $strA area code matches with @No, I can do: if ( (rindex($str_No,substr($strA,0,5))))== -1) ) { print "Not... (1 Reply)
Discussion started by: jimmy_y
1 Replies

9. Shell Programming and Scripting

Remove Garbage Output

Hello Friends, In a script i m using different temporary file and i remove them in the end. During script execution i have some garbage output which is not required. For example: Garbage Output ++ rm temp_out temp_a temp_b temp_c ++ rm Filter1 Filter2 Script : Even i am redirecting rm... (7 Replies)
Discussion started by: Danish Shakil
7 Replies

10. Shell Programming and Scripting

How do i remove the unneccesary output ?

Hi , I have written a csh script which is as follows but it keeps outputing a unneccesary output as show below. How can i remove that output and prevent it from displaying it when the script runs? set USER = "aaa" set PASSWORD = "bbb" ftp -n 10.80.12.18<<XX user $USER $PASSWORD... (2 Replies)
Discussion started by: Raynon
2 Replies
Login or Register to Ask a Question