gAWK - Replacing space with commas

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

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 0.0.0.0 -port@1 20 300 4000 50000 ; 0.0.0.0 -port@6 70 800

line would appear
textextext 0.0.0.0 -port@1,20,300,4000,50000 ; 0.0.0.0 -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,

Matt


See More: gAWK - Replacing space with commas

Report •


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

example

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

GNU win32 packages | Gawk


Report •
Related Solutions


Ask Question