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; }