Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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,800I'd prefer it done using gawk rather then sed, but sed script would be welcome too.
Cheers and thanks for your time,
Matt

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

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |