awk gsub command to replace multiple spaces

Hi Forum.

I'm trying to cleanup the following data elements (To remove any occurences of commas and any extra spaces) while preserving the <TAB> delimiter using awk gsub but I have not been successful.

Original Data:
4365 monte des source rue,,     ,<TAB>trevost<TAB>QC

Desired Data:
4365 monte des source rue<TAB>trevost<TAB>QC
I have tried the following but they don't give the desired results:

awk '{gsub(", *, *,\t","\t",$0); print;}'
awk '{gsub(", +, +,\t","\t",$0); print;}'

awk '{gsub(/, *, *,\t/,"\t",$0); print;}'
awk '{gsub(/, +, +,\t/,"\t",$0); print;}'

Can you please help?

Thanks.

Hi, try:

awk '{sub(/[ ,]+$/,"",$1); print}' FS='\t' OFS='\t'  

or

awk '{sub(/[ ,]+\t/,"\t"); print}' 
1 Like