-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtbl2x
executable file
·113 lines (107 loc) · 2.11 KB
/
tbl2x
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl
my $PAD = 20;
my $numbers = 0;
my $re;
my $x;
my $quote;
my $cmt;
if (scalar(@ARGV) && $ARGV[0] =~ /^\-/) {
my $opt = shift @ARGV;
if ($opt eq '--hdr') {
$hdr = join("\t",split(/,/,shift @ARGV));
}
elsif ($opt eq '--hdrfile') {
my $fn = shift @ARGV;
open(F,$fn) || die $fn;
$hdr = <F>;
close(F);
}
elsif ($opt eq '-a' || $opt eq '--autohdr') {
$hdr = join("\t", (1 .. shift @ARGV));
}
elsif ($opt eq '--pad') {
$PAD = shift @ARGV;
}
elsif ($opt eq '-r') {
$re = shift @ARGV;
}
elsif ($opt eq '-x') {
$x = shift @ARGV;
}
elsif ($opt eq '--quote') {
$quote = 1;
}
elsif ($opt eq '--comment' or $opt eq '-c') {
$cmt = shift @ARGV;
}
elsif ($opt eq '--hash-comment' or $opt eq '-H') {
$cmt = '#';
}
elsif ($opt eq '-n') {
$numbers = 1;
}
else {
die $opt;
}
}
while (!$hdr) {
$hdr = <>;
if ($cmt && index($hdr, $cmt) == 0) {
$hdr = undef;
}
}
chomp $hdr;
$hdr =~ s/\r//g;
@hdrs = split(/\t/,$hdr);
my @lines = ();
if ($x) {
my $tmpf = "/tmp/tbl2x-$$.tsv";
open(F,">$tmpf") || die $tmpf;
while(<>) {
if ($cmt && index($_, $cmt) == 0) {
continue;
}
print F $_;
}
close(F);
open(F, "cat $tmpf | $x|") || die;
@lines = <F>;
close(F);
unlink($tmpf);
}
else {
@lines = <>;
}
$line = 0;
while ($_ = shift @lines) {
if ($cmt && index($_, $cmt) == 0) {
next;
}
$line++;
if ($re) {
if ($_ !~ /$re/) {
next;
}
}
print "RECORD: $line\n";
chomp;
s/\r//g;
@vals = split(/\t/,$_);
for ($i=0;$i<@hdrs;$i++) {
if ($numbers) {
printf "%2d ",$i+1;
}
my $fmt = "%".$PAD."s";
$col = $hdrs[$i];
if (length($col) > $PAD) {
$col = substr($col, 0, $PAD);
}
$v = $vals[$i];
if ($quote) {
$v = '"' . $v . '"';
}
printf "$fmt: %s\n", $col, $v;
}
print "\n";
print "\n";
}