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

    Your code work great, except for it dies on the first row containing double-quotes on the 3rd field in the input file.

    It processes the first 67 rows, puts double-quotes around all fields and changes the delimiter to |.

    i.e. 988A1093|" 98_08_NR"|" Environmental Affairs"|" false"|" store"|" false"

    Not sure how to handle. Thanks, Mike

      What does "dies on the first row" mean? Does it give you an error message? Can you show the input line that it dies on, perhaps with a couple lines before and after it for context? Also, check the documentation for Text::CSV for info on using binary or different encodings, in case your input has any characters that are not simple ASCII text.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

        By die, I mean the script just stops with no error messages.

        It creates the first 67 rows until row 68 which is the first row with double quotes around the field with the comma imbedded within the quotes. See row 3 and 4 of data below

        988A5370, 98_1V_HB, ADVANCED ROTORCRAFT SYSTEMS, false, store, false, +, , , , unspecified, USD, false, +, true, +, false, +, false, +, fals +e, + 988A5521, 98_1V_HB, Hel Product Pool (Pds Dd), false, store, false, , +, , , unspecified, USD, false, +, true, +, false, +, false, +, false, + + 988A5707, 98_1V_HB, "Chinook, IPT ME Support", false, store, false, , +, , , unspecified, USD, false, +, true, +, false, +, false, +, false, + + 988A5708, 98_1V_HB, "Chinook, ME Factory Supt", false, store, false, , + , , , unspecified, USD, false, +, true, +, false, +, false, +, false +, + 988A5761, 98_1V_HB, Tandem Rotor Configuration Mgt, false, store, fals +e, , , , , unspecified, USD, false, +, true, +, false, +, false, +, f +alse, + 988A5762, 98_1V_HB, RECAP-Product Data Management, false, store, false +, , , , , unspecified, USD, false, +, true, +, false, +, false, +, fa +lse, +

        Any suggestions would be great. Thanks, Mike