http://www.perlmonks.org?node_id=1001690


in reply to Regular Expressions - replacing newlines and carriage returns inside quotes

Text::CSV is out of the question, too? I tried to do some "quick n dirty" CSV manipulation much like this recently but as usual things quickly got much more dirty than quick, I said bleep this, I'll use CPAN, and things were peachy. The main part of the resulting script looks like this:

my $csv = Text::CSV->new({ binary => 1, sep_char => "\t", always_quote => 1, }) or do { print STDERR "Cannot initialize CSV: ", Text::CSV->error_diag, "\n +"; exit 1; }; LINE: while (my $row = $csv->getline(\*STDIN)) { s/\n/$replace/g foreach(@$row); unless($csv->combine(@$row)) { print STDERR "Error converting record $. for output: ", $csv-> +error_input, "\n"; next LINE; } print $csv->string, "\n"; } $csv->eof or $csv->error_diag;

I tried getting your code to work but couldn't, maybe for lack of CRs in my source, but I'd doubt it covers all the subtleties CSV allows regarding quoting and escaping, let alone those it doesn't allow but you'll find anyway.

Edit: I think we got ourselves a consensus here :-D