mmbb has asked for the wisdom of the Perl Monks concerning the following question:
Sample input records: ONE,"TWo,2",Three,Four,Five ONE,"$TWo,2",Three,Four,Five ONE,"$TWo2","Three,(3)",Four,Five ONE,"$TWo2","Three,(3",Four,Five Expected output records: ONE,"TWo|2",Three,Four,Five ONE,"$TWo|2",Three,Four,Five ONE,"$TWo2","Three|(3)",Four,Five ONE,"$TWo2","Three|(3",Four,Five Current output: ONE,"TWo|2",Three,Four,Five --> ok, converted ONE,"$TWo,2",Three,Four,Five --> wrong, not converted ONE,"$TWo2","Three,(3)",Four,Five --> wrong, not converted Got error for record 4: Unmatched ( in regex; marked by <-- HERE in m/"Three,( <-- HERE 3"/ at + PerlComma.pl line 20, <> line 4 #!/usr/bin/perl use strict; use warnings; my $line; my @matchlist; my $rplstr; while (<>) { $line=$_; @matchlist = $line =~ m/("[^"]*")/g; for my $matchstr (@matchlist) { $rplstr=$matchstr; $rplstr =~ tr/[,]/[|]/; s:$matchstr:$rplstr:g } print; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Search & Replace doesn't work with string containing certain special characters
by jellisii2 (Hermit) on Sep 09, 2014 at 11:26 UTC | |
|
Re: Search & Replace doesn't work with string containing certain special characters
by wrog (Friar) on Sep 09, 2014 at 11:03 UTC | |
|
Re: Search & Replace doesn't work with string containing certain special characters
by Anonymous Monk on Sep 09, 2014 at 09:23 UTC | |
by mmbb (Initiate) on Sep 09, 2014 at 09:42 UTC |