Computing.Net > Forums > Programming > gAWK - Replacing space with commas

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

gAWK - Replacing space with commas

Reply to Message Icon

Name: gustisok
Date: September 19, 2009 at 10:53:50 Pacific
OS: Linux
Subcategory: General
Tags: gawk, script, replace, space, comma
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: September 19, 2009 at 18:22:00 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: gAWK - Replacing space with commas

Replace blank spaces with ; in a tx www.computing.net/answers/programming/replace-blank-spaces-with-in-a-tx/13192.html

View installed progs with .bat file www.computing.net/answers/programming/view-installed-progs-with-bat-file/14550.html

Batch File-change space with tab www.computing.net/answers/programming/batch-filechange-space-with-tab-/16753.html