sed help needed - replacing parts of a string 
Author Message
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 
 [ 8 post ] 

 Relevant Pages 

1. need help on using Ruby to replace some SED expressions

2. REPLACE part of a string

3. replacing a part of a string using a regexp

4. string.replace() can't replace newline characters???

5. sed (replace extra space) with pipe delimiter

6. Sed script to replace nth instance of >?

7. Sed/AWK - replace positions in multiple lines.

8. Need help with sed or awk !!

9. Need awk (or sed) help PLEASE!

10. new to sed/awk, need help badly

11. Sed/Awk help needed

12. Help, how to replace string in multiple files

 

 
Powered by phpBB® Forum Software