Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

find and replace using awk?

Posted on 2008-11-05
11
Medium Priority
?
724 Views
Last Modified: 2013-12-26
Hi, can some one let me know how I can use an awk script to be able to search every line in the file and find the state and zip code and insert a comma(,) between the state and zip fields. My file sample is shown below. Thanks for the  help.
john, doe, MO 78985
Mary, decker, TX 89989
Jason ,smith ,789 3r;st, NY 10009
charles, NY 10007

Open in new window

0
Comment
Question by:aej1973
11 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 22887743
awk '{$NF=", " $NF; print}'
0
 
LVL 85

Expert Comment

by:ozo
ID: 22887794
awk '$(NF-1)=$(NF-1) ","'
0
 

Author Comment

by:aej1973
ID: 22887888
I did not get you. is it

awk '$(NF-1)=$(NF-1) ","'' file.txt
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 85

Expert Comment

by:ozo
ID: 22887937
yes
0
 

Author Comment

by:aej1973
ID: 22888133
ozo, this does not seem to be working. My actual data is as attached. The comma(,) is falling between the telephone number (ie) 317, 435-0000, but I need it to fall between state and zip (ie) TX , 66312-1111. Thanks for the help.

A
0900-005550 , 755;MORTIMER ST ,BAY,TX 66312-1111 , 317 435-0000
0900-005556 , 907;HULL ST ,BAY,IL 63312-1111 , 317 363-1000
0440-005559 , 620;PERRY ST ,NON PAY BACK BALANCE :BAR,MO 66312
0881-005552 , 350;ONE ST;APT 8 ,RAY,IL 63312 , 317 335-2456
0961-005551 , 1211;ROD ST ,RAY,IL 62312-1372 , 317 335-2400
0711-005553 , 438;BAINBRIDGE ST ,RAY,IL 62312-1098 , 317 335-0123

Open in new window

0
 
LVL 85

Accepted Solution

by:
ozo earned 600 total points
ID: 22888238
awk '{p=match($0,/[A-Z][A-Z] +[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]/);print substr($0,1,p+1) "," substr($0,p+2)}' file.txt
0
 

Author Comment

by:aej1973
ID: 22888446
ozo, you are the best! Thank you so much. Before I close this question can I ask you suggest a good reference manual /study guide for awk scripting that I can use. Thanks again.

a
0
 
LVL 85

Expert Comment

by:ozo
ID: 22888496
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22888609
good reference for awk:

http://www.grymoire.com/Unix/Awk.html
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22889204
Another option is to use sed, eg:

sed "s/\(.*[A-Z]\)/\1,/" file.txt
0
 

Author Closing Comment

by:aej1973
ID: 31513584
Awesome work, keep it up.

A
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question