Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reg exp to parse a csv

Status
Not open for further replies.

SM777

Technical User
Mar 7, 2001
208
GB
I have a csv file which I want to strip out the " quotes. The problem is that some of the fields have , in the middle of them e.g. 3 fields:

"intel","fast, expensive","$45"

when I replace s/"//g I end up with 4 fields:

intel,fast, expensive,$45

I guess I need to strip out the first " on the first char of a row, then any s/",// then s/,"// then the physical last " on the row.

Is this the right way? how do I detect the first and last "
Another way I was thinking of is to change all the , which are not surrounded by " with a ; so I then get:

"intel";"fast, expensive";"$45"

and then I can just do a s/"//g to get

intel;fast, expensive;$45


Cheers.
 
SM777

Something like the following should work. I'm sure some regexp guru can provide an even more compact solution:

Code:
while(<>) {

    chomp( @fields = split /&quot;,&quot;/ );

    $fields[0]        =~ s/&quot;//;
    $fields[$#fields] =~ s/&quot;//;

    print join( &quot;;&quot;, @fields ), &quot;\n&quot;;
}

Cheers, NEIL s-)
 
If you are encountering something like the one below it might help you..

$temp='&quot;intel&quot;,&quot;fast,expensive&quot;,&quot;$45&quot;';
$_=$temp;
$pattern='&quot;,&quot;';
$replacewith=&quot;;&quot;;

s/$pattern/$replacewith/g;
s/&quot;//g;
print($_.&quot;/n&quot;);

Good luck
ganesh :-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top