in reply to Re: swapping PIPE for comma in CSV file
in thread swapping PIPE for comma in CSV file
For the OP just to emphasize the importance of binary in the case you have a def. of CSV that permits embedded newlines.
For Merijn.
I was going to answer more or less the same to the OT yesterday, but came across a few problems, that made me reinstall the latest versions...
cheers --stephan p.s I tested on cygwin with perl 5.8.7 and 5.8.8 update: oops forgot the code...% steph@apexPDell2 (/home/stephan) % % cat conv_comma2pipe_xs.px #!/usr/bin/perl use strict; use warnings; $|++; #use IO::Handle; use IO::Wrap; use Text::CSV_XS; # use DDS; # my $in = IO::Wrap::wraphandle(\*STDIN) or die; # my $out = IO::Wrap::wraphandle(\*STDOUT) or die; # Dump\($in, $out); my $csv_in = Text::CSV_XS->new({ binary => 1, }) or die; my $csv_out = Text::CSV_XS->new({ binary => 1, sep_char => q{|}, eol => qq{\n}, }) or die; while (defined (my $rec = $csv_in->getline(\*STDIN)) ) { { my @fields = @$rec; local $"=q{][}; print {\*STDERR} ".rec [@fields]\n"; } $csv_out->print(\*STDOUT, $rec); } __END__ % steph@apexPDell2 (/home/stephan) % % cat hi1.csv | perl+ -w conv_comma2pipe_xs.px .rec [a][b][c] a|b|c .rec [a][okay, comma][c] a|"okay, comma"|c .rec [a][long line, indeed][end] a|"long line, indeed"|end % steph@apexPDell2 (/home/stephan) % % cat hi1.csv a,b,c a,"okay, comma",c a,"long line, indeed",end
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: swapping PIPE for comma in CSV file
by Tux (Canon) on Jun 28, 2007 at 07:00 UTC |