c:\@Work\Perl\monks>perl -wMstrict -le "my $s = '65722417,\"1193,1\",1012,\"9,8,7\",C2,Carrier Cost Recovery +Fee,0.0273'; print qq{'$s'}; ;; $s =~ s{ (\" [^^\x22]+ \") }{ (my $one = $1) =~ s{,}{-}xmsg; $one; } +xmsge; print qq{'$s'}; " '65722417,"1193,1",1012,"9,8,7",C2,Carrier Cost Recovery Fee,0.0273' '65722417,"1193-1",1012,"9-8-7",C2,Carrier Cost Recovery Fee,0.0273'
If you have Perl 5.14+, there is a slightly simpler version of the  s{,}{-}xmsg substitution that uses the  /r modifier. See  s/PATTERN/REPLACEMENT/msixpodualgcer in the Regexp Quote-Like Operators section of perlop.

Update: I use  [^\x22] in the char class in the  (\" [^^\x22]+ \") capture of the substitution only because my REPL doesn't like unbalanced double-quote characters! Note also that in the Windows command line, embedded double-quotes must be escaped, and that certain other characters such as ^ must be conditionally escaped. So the true version of the regex capture sub-expression is  (" [^\x22]+ ") or more simply  (" [^"]+ ") instead. Also note that this regex does not handle escaped characters within the quoted string! Also note that  tr{,}{-} (or  tr{,}{-}r with 5.14+) might be a bit quicker.


Give a man a fish:  <%-(-(-(-<


In reply to Re: Modifying CSV File by AnomalousMonk
in thread Modifying CSV File by roho

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.