in reply to Modifying CSV File
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.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'
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: <%-(-(-(-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Modifying CSV File
by roho (Bishop) on Jun 16, 2015 at 16:49 UTC |