sed help needed - replacing parts of a string
Author |
Message |
Paul E Lehman #1 / 8
|
 sed help needed - replacing parts of a string
I am trying to create a comma delimited file from a file that has lines like the one below: SOMC2 C024 "SOMERSET 1 ENE; CO" 9709295691 1 300 I would like the line to look like: SOMC2,C024,SOMERSET 1 ENE; CO, 970929569,1,1,300 How do I replace spaces in all fields EXCEPT the spaces in the "quoted field" ?
|
Sat, 08 Feb 2003 03:00:00 GMT |
|
 |
Kenny McCorma #2 / 8
|
 sed help needed - replacing parts of a string
Quote: >I am trying to create a comma delimited file from a file that has lines like >the one below: >SOMC2 C024 "SOMERSET 1 ENE; CO" 9709295691 1 300 >I would like the line to look like: >SOMC2,C024,SOMERSET 1 ENE; CO, 970929569,1,1,300 >How do I replace spaces in all fields EXCEPT the spaces in the "quoted >field" ?
Since this is comp.lang.AWK (emphasis mine), here's one solution: BEGIN { ORS="";FS="\"" } { for (i=1; i<=NF; i++) { if (i % 2) gsub(/ +/,",",$i) print $i } print "\n" Quote: }
|
Sat, 08 Feb 2003 03:00:00 GMT |
|
 |
Harlan Grov #3 / 8
|
 sed help needed - replacing parts of a string
<snip> Quote: >BEGIN { ORS="";FS="\"" } >{ >for (i=1; i<=NF; i++) { > if (i % 2) gsub(/ +/,",",$i) > print $i > } >print "\n" >}
Points off for excessive testing and i/o! BEGIN { FS = "\""; OFS = "" } { for (i = 1; i <= NF; i += 2) gsub(/ +/, ",", $i); print } Sent via Deja.com http://www.deja.com/ Before you buy.
|
Sat, 08 Feb 2003 03:00:00 GMT |
|
 |
Paul E. Lehman #4 / 8
|
 sed help needed - replacing parts of a string
Thanks for trying to help but it does not work. Have you tried it on a test file?
Quote:
> >I am trying to create a comma delimited file from a file that has lines like > >the one below: > >SOMC2 C024 "SOMERSET 1 ENE; CO" 9709295691 1 300 > >I would like the line to look like: > >SOMC2,C024,SOMERSET 1 ENE; CO, 970929569,1,1,300 > >How do I replace spaces in all fields EXCEPT the spaces in the "quoted > >field" ? > Since this is comp.lang.AWK (emphasis mine), here's one solution: > BEGIN { ORS="";FS="\"" } > { > for (i=1; i<=NF; i++) { > if (i % 2) gsub(/ +/,",",$i) > print $i > } > print "\n" > }
|
Sat, 08 Feb 2003 03:00:00 GMT |
|
 |
Paul E. Lehman #5 / 8
|
 sed help needed - replacing parts of a string
Thanks for trying to help but it does not work.
Quote:
> <snip> > >BEGIN { ORS="";FS="\"" } > >{ > >for (i=1; i<=NF; i++) { > > if (i % 2) gsub(/ +/,",",$i) > > print $i > > } > >print "\n" > >} > Points off for excessive testing and i/o! > BEGIN { FS = "\""; OFS = "" } > { for (i = 1; i <= NF; i += 2) gsub(/ +/, ",", $i); print } > Sent via Deja.com http://www.deja.com/ > Before you buy.
|
Sat, 08 Feb 2003 03:00:00 GMT |
|
 |
Harlan Grov #6 / 8
|
 sed help needed - replacing parts of a string
Quote: >Thanks for trying to help but it does not work.
<snip> How so? Running bash under linux, % cat infile SOMC2 C024 "SOMERSET 1 ENE; CO" 9709295691 1 300 SOMC2 "C024 SOMERSET 1 ENE;" CO 9709295691 1 300 SOMC2 C024 "SOMERSET 1 ENE; CO 9709295691" 1 300 "SOMC2 C024 SOMERSET 1 ENE; CO 9709295691 1 300" SOMC2 C024 SOMERSET 1 ENE; CO"" 9709295691 1 300 % gawk 'BEGIN { FS = "\""; OFS = "" } Quote: > { for (i = 1; i <= NF; i += 2) gsub(/ +/, ",", $i); print }' infile
SOMC2,C024,SOMERSET 1 ENE; CO,9709295691,1,300 SOMC2,C024 SOMERSET 1 ENE;,CO,9709295691,1,300 SOMC2,C024,SOMERSET 1 ENE; CO 9709295691,1,300 "SOMC2 C024 SOMERSET 1 ENE; CO 9709295691 1 300" SOMC2,C024,SOMERSET,1,ENE;,CO,9709295691,1,300 % The fact that gawk isn't handling the 4th line correctly is a problem (and one I'm not going to try to figure out tonight). It could be that your shell is mishandling the FS assignment. Try putting the script in a file (eg, scriptfile), and running as gawk -f scriptfile infile Sent via Deja.com http://www.deja.com/ Before you buy.
|
Sun, 09 Feb 2003 12:45:15 GMT |
|
 |
Paul E Lehman #7 / 8
|
 sed help needed - replacing parts of a string
My apologies: I tried running what you sent as an awk script instead of command line and the script was {*filter*} on the ' on line one. I have it working now and it does what I need. Thanks for your help Paul
Quote:
> >Thanks for trying to help but it does not work. > <snip> > How so? Running bash under linux, > % cat infile > SOMC2 C024 "SOMERSET 1 ENE; CO" 9709295691 1 300 > SOMC2 "C024 SOMERSET 1 ENE;" CO 9709295691 1 300 > SOMC2 C024 "SOMERSET 1 ENE; CO 9709295691" 1 300 > "SOMC2 C024 SOMERSET 1 ENE; CO 9709295691 1 300" > SOMC2 C024 SOMERSET 1 ENE; CO"" 9709295691 1 300 > % gawk 'BEGIN { FS = "\""; OFS = "" } > > { for (i = 1; i <= NF; i += 2) gsub(/ +/, ",", $i); print }' infile > SOMC2,C024,SOMERSET 1 ENE; CO,9709295691,1,300 > SOMC2,C024 SOMERSET 1 ENE;,CO,9709295691,1,300 > SOMC2,C024,SOMERSET 1 ENE; CO 9709295691,1,300 > "SOMC2 C024 SOMERSET 1 ENE; CO 9709295691 1 300" > SOMC2,C024,SOMERSET,1,ENE;,CO,9709295691,1,300 > % > The fact that gawk isn't handling the 4th line correctly is a problem > (and one I'm not going to try to figure out tonight). > It could be that your shell is mishandling the FS assignment. Try > putting the script in a file (eg, scriptfile), and running as > gawk -f scriptfile infile > Sent via Deja.com http://www.*-*-*.com/ > Before you buy.
|
Sun, 09 Feb 2003 03:00:00 GMT |
|
|
|