in reply to Converting File Delimiters
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Converting File Delimiters
by mmueller44 (Novice) on Aug 13, 2012 at 19:57 UTC | |
by aaron_baugher (Curate) on Aug 13, 2012 at 21:52 UTC | |
by mmueller44 (Novice) on Aug 16, 2012 at 16:40 UTC | |
by Tux (Canon) on May 22, 2013 at 06:42 UTC | |
by aaron_baugher (Curate) on Aug 20, 2012 at 17:29 UTC |