FatDog has asked for the wisdom of the Perl Monks concerning the following question:

I have a file where each row is 1 record with about 65 columns of information. I need to test some of these columns for specific ranges of values. So I naturally want to use split like this:
my (@Cols) = split (",", $lRow);
Here is the problem: many of the rows have free-text comment blocks with commas that are delimited by double-quotes like this:
1123,,,"10.11.12.13",4,"I really, really wanted",,,"It was, like great +" 1123,,,"34.123.12.12",8,"Fantastic",,,"",,
How do I remove all commas if they are between ," and ", bracketing pairs?

Note: I dont have to preserve the free-text comments. Is there a regrex to detect & replace everything between: ," and ", if there is a comma with ,"",? The problem is: things like IP addresses and email address are also enclosed in " characters and I need to preserve them. Another problem, some people put multiple comma's in their comments like: "It was ,,,,, the worst experience of my life"

Note: I can make multiple passes through the row so I have thought about replacing all ," combinations with ,| combinations and all ", combinations with |, then replacing everything between two || chars with "".

Replies are listed 'Best First'.
Re: Need help parsing comma-delimited file
by jZed (Prior) on May 20, 2004 at 23:30 UTC
Re: Need help parsing comma-delimited file
by pizza_milkshake (Monk) on May 20, 2004 at 22:40 UTC
    perl -MCPAN -e'install Text::CSV' ... #!perl -wl use strict; use Text::CSV; my $csv = new Text::CSV; while (<DATA>) { next unless $csv->parse($_); print join(":", $csv->fields()); } __DATA__ "1","2",3 "a,b",c,d

    perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"

      Cool. I will install Text::CSV and give it a shot. Thanks!
Re: Need help parsing comma-delimited file
by Nkuvu (Priest) on May 20, 2004 at 22:06 UTC