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


In reply to Re: Regular Expressions - replacing newlines and carriage returns inside quotes by mbethke
in thread Regular Expressions - replacing newlines and carriage returns inside quotes by PRyanRay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.