Skip to content

Instantly share code, notes, and snippets.

@samvdb
Created December 17, 2018 13:34
#!/usr/bin/perl
# This script can be used as a base to parse unreliable CSV streams
# Modify to your own needs
#
# (m)'08 [23 Apr 2008] Copyright H.M.Brand 2008-2018
use strict;
use warnings;
sub usage {
my $err = shift and select STDERR;
print <<"EOH";
usage: $0 [-o file] [-s S] [file]
-o F --out=F output to file F (default STDOUT)
-s S --sep=S set input separator to S (default ; , TAB or |)
EOH
exit $err;
} # usage
use Getopt::Long qw(:config bundling);
GetOptions (
"help|?" => sub { usage (0); },
"s|sep=s" => \my $in_sep,
"o|out=s" => \my $opt_o,
) or usage (1);
use Text::CSV_XS qw( csv );
my $io = shift || \*DATA;
my $eol = "\n";
binmode STDOUT, ":encoding(utf-8)";
my @hdr;
my @opt_i = (
in => $io,
binary => 1,
blank_is_undef => 1,
allow_loose_quotes => 1,
allow_loose_escapes => 1,
sep => ";",
encoding => "utf16le",
);
my @opt_o = (out => \*STDOUT, eol => $eol, sep => ",", quo => '"', always_quote => 1,);
push @opt_i,
bom => 1,
sep_set => [ $in_sep ],
keep_headers => \@hdr;
push @opt_o,
headers => \@hdr;
csv (in => csv (@opt_i), @opt_o);
__END__
a;b;c;d;e;f
"test"ja ze";2;3;4;5;6
2;3;4;5;6;7
3;4;5;6;7;8
4;5;6;7;8;9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment