-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreformat_csv.pl
executable file
·35 lines (28 loc) · 1.11 KB
/
reformat_csv.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env perl
use strict;
use warnings;
use Text::CSV_XS;
use 5.010;
use Getopt::Long qw(GetOptions);
my $input_sep = ',';
my $input_esc = '"';
my $output_sep = ',';
my $output_esc = '"';
GetOptions(
'input_sep=s' => \$input_sep,
'input_esc=s' => \$input_esc,
'output_sep=s' => \$output_sep,
'output_esc=s' => \$output_esc )
or die "Usage: $0 --input_sep INPUT SEPERATOR --input_esc INPUT ESCAPE --output_sep OUTPUT SEPERATOR --output_esc OUTPUT ESCAPE FILES";
my $csv = Text::CSV_XS->new ( { binary => 1, allow_whitespace => 1, allow_loose_quotes => 1, escape_char => $input_esc, sep_char => $input_sep } )
or die "Cannot use CSV: ".Text::CSV_XS->error_diag ();
my $outcsv = Text::CSV_XS->new ( { binary => 1, allow_whitespace => 1, allow_loose_quotes => 1, escape_char => $output_esc, sep_char => $output_sep } )
or die "Cannot use CSV: ".Text::CSV_XS->error_diag ();
binmode(STDOUT, ":encoding(UTF-8)");
foreach my $infile (@ARGV) {
open my $fh, "<:encoding(UTF-8)", $infile or die "Can't open csv $infile";
while (my $row = $csv->getline($fh)) {
$outcsv->say (*STDOUT, $row);
}
close $fh;
}