-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv2tab
executable file
·40 lines (35 loc) · 860 Bytes
/
csv2tab
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
35
36
37
38
39
40
#!/usr/bin/perl
use strict;
use Getopt::Std;
use FindBin;use lib $FindBin::Bin;
my $usage = q/Usage:
csv2tab [-n][-H] input.csv
Converts a csv to a tab delimited output, removing double quotes
around every field.
Options:
-n : disable header
-H : show only the header
/;
umask 0002;
getopts('nHo:') || die($usage."\n");
my $outfile=$Getopt::Std::opt_o;
my ($nohdr, $hdronly)=($Getopt::Std::opt_n, $Getopt::Std::opt_H);
if ($outfile) {
open(OUTF, '>'.$outfile) || die("Error creating output file $outfile\n");
select(OUTF);
}
# --
my $ln=0;
while(<>) {
$ln++;
next if ($ln==1 && $nohdr);
my $re = qr/,(?=(?:[^"]*"[^"]*")*(?![^"]*"))/;
print join "\t", map { s/(?<!\\)"//gr =~ s/\\"/"/gr } split $re;
exit if ($hdronly);
}
# --
if ($outfile) {
select(STDOUT);
close(OUTF);
}
#************ Subroutines **************