-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtblabelize
executable file
·48 lines (41 loc) · 1.08 KB
/
tblabelize
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
#!/usr/bin/perl
# tblabelize: label columns of an unlabeled file
# Copyright(c) 2008-2009 EURAC, Institute of Genetic Medicine
use strict;
use warnings;
use locale;
use Getopt::Std;
my %flags;
getopts("zn:", \%flags);
my $SEP = ($ENV{'TBLSEP'} || "\t");
my ($file, @names) = @ARGV;
if(!$file)
{
print(STDERR qq{$0 [-zN] file [column names]:
Assign numerical or custom labels to an unlabeled CSV file.
CSV files are TAB separated by default. You can change the column separator
by setting the TBLSEP environment variable.
-z: start from zero
-n N: start from N
});
exit(2);
}
# open the file and read columns
open(FD, $file) or die("cannot open $file\n");
$_ = <FD>;
s/[\r\n]+$//;
my @cols = split($SEP, $_, -1);
# assign names
die("too many columns given on the command line") if($#names > $#cols);
my @res = @names;
my $z = (defined($flags{'z'})? 0: defined($flags{'n'})? $flags{'n'}: 1);
foreach my $n(1 .. ($#cols - $#res))
{
my $cn = $n + $#names + $z;
push(@res, $cn);
}
# output
print(join($SEP, @res) . "\n");
print(join($SEP, @cols) . "\n");
print(<FD>) while(<FD>);
close(FD);