You could let a module do the work of keeping the quotes and delimiters straight:
#!/usr/bin/env perl use Modern::Perl; use Text::CSV; my $ic = Text::CSV->new() or die Text::CSV->error_diag(); my $oc = Text::CSV->new({sep_char => '|', eol => $/ }) or die Text::CSV->error_diag(); open my $if, '<', 'infile' or die $!; open my $of, '>', 'outfile' or die $!; while( my $r = $ic->getline($if)){ $oc->print($of, $r); } close $if; close $of;
Update: I'm not normally a "use modules for everything" kind of guy, but this is one task where I think it's a no-brainer. Note that all the other solutions above will give you a borked output file if any of your non-quoted fields contain a pipe character. Of course, you may know that will never be an issue with your data. But in general, letting the module handle quoting on both input and output is the safest bet.
Aaron B.
Available for small or large Perl jobs; see my home node.
In reply to Re: Converting File Delimiters
by aaron_baugher
in thread Converting File Delimiters
by mmueller44
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |