Problem using gsub in gawk

I know that most of you guys probably won't reply to this, because you'll feel like my post doesn't even deserve a glance. However, for those of you that do decide to help- thank you!

TRAILS TA
2700 E Main St
ALBERT LEA , MN 56007
 507-373-4200

TRUCKER'S INN
State Hwy 30 AT I 35 N
ELLENDALE , MN 56026
 507-688-9433

and I want it to look like this at the end..

TRAILS T|2700 E Main St|ALBERT LEA|MN|56007|507373420|
TRUCKER'S IN|State Hwy 30 AT I 35 N|ELLENDALE|MN|56026|507688943|

I am obviously a beginner with gawk. I have started out by using simple things like
gsub(/-/, "") but all that it gives me is the numbers, and not any of my other information.
I have no idea what I'm doing, so any help getting me to my eventual goal would be greatly appreciated! Even if it is, "you suck, start out by doing this instead".

Again, thanks!

nice attempt at being subliminal...

nawk '$1=$1' RS='' OFS='|' myFile
1 Like

also try:

awk '
/^ *$/{gsub("[-]","",l);gsub(" *[|] *","|",l);sub("^[|]*","",l);print l; l="";}
/, *.. [0-9][0-9]*$/ {sub(",","|"); $NF="|"$NF}
{l=l $0 "|";}
' infile
1 Like

Thank you both so much!

awk '
/^ *$/{gsub("[-]","",l);gsub(" *[|] *","|",l);sub("^[|]*","",l);print l; l="";}
/, *.. [0-9][0-9]*$/ {sub(",","|"); $NF="|"$NF}
{l=l $0 "|";}
' infile

This works perfectly, except that I need to get rid of gaps in the phone number like

Speedy Mart #19|US 127 1009 Holmes St|Frankfort|KY|40601|502 8750778|
Marathon 150 Quick Stop|Hwy 150 Exit 25|Bardstown|KY|4004|502 3484930|
Love's Travel Stop #360|Hwy 218 Exit 58|Horse Cave|KY|42749|270 7864000|
Liberty Fuel|US 641 SR 70|Marion|KY|42064|270 9654922|

I've tried adding:

awk '
/^ *$/{gsub("[-]","",l);gsub(" *[|] *","|",l);sub("^[|]*","",l);gsub(([0-9][0-9][0-9]) ([0-9][0-9][0-9][0-9][0-9][0-9][0-9]), "" print l; l="";}
/, *.. [0-9][0-9]*$/ {sub(",","|"); $NF="|"$NF}
{l=l $0 "|";}
' infile 

It was finding the occurrences with my regular expression, but I don't know how to say "find the space between these numbers, but keep the numbers and delete the space". Any ideas?

try:

awk '
/^ *$/{gsub("[-]","",l);gsub(" *[|] *","|",l);sub("^[|]*","",l);print l; l="";}
/, *.. [0-9][0-9]*$/ {sub(",","|"); $NF="|"$NF}
/^ *[0-9 -][0-9 -]* *$/ {gsub("[ -]","")}
{l=l $0 "|";}
' infile
1 Like

Another one:

awk '{gsub(/ /,"|",$4); gsub(/[ -]/,x,$5)}NF++' FS=' , |\n' RS='' OFS='|' infile