gAWK - Replacing space with commas

September 19, 2009 at 10:53:50
Specs: Linux

I need some help with a script that would find certain word in a line (-port@) , then replace all spaces after that line with commas until it encounters a number followed by a dot, semi-colons or end of line.

I am using " " as field separators.

So, for example

textextext -port@1 20 300 4000 50000 ; -port@6 70 800

line would appear
textextext -port@1,20,300,4000,50000 ; -port@6,70,800

I'd prefer it done using gawk rather then sed, but sed script would be welcome too.

Cheers and thanks for your time,


See More: gAWK - Replacing space with commas

Report •

September 19, 2009 at 18:22:00
set your Field separator as "-port@" ...then go through each field using for loop


# echo "textextext -port@1 20 300 4000 50000 ; -port@6 70 800" | awk 'BEGIN{FS="-port@"}{for(i=2;i<=NF;i+=1){gsub(/ /,",",$i)}}1'  OFS="-port@"
textextext -port@1,20,300,4000,50000,;,,-port@6,70,800

GNU win32 packages | Gawk

Report •
Related Solutions

Ask Question