find and replace using awk?

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

aej1973Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
awk '{$NF=", " $NF; print}'
0
ozoCommented:
awk '$(NF-1)=$(NF-1) ","'
0
aej1973Author Commented:
I did not get you. is it

awk '$(NF-1)=$(NF-1) ","'' file.txt
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

ozoCommented:
yes
0
aej1973Author Commented:
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
ozoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aej1973Author Commented:
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
ozoCommented:
0
omarfaridCommented:
good reference for awk:

http://www.grymoire.com/Unix/Awk.html
0
TintinCommented:
Another option is to use sed, eg:

sed "s/\(.*[A-Z]\)/\1,/" file.txt
0
aej1973Author Commented:
Awesome work, keep it up.

A
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.