-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtblsize
executable file
·49 lines (43 loc) · 941 Bytes
/
tblsize
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
#!/usr/bin/env perl
# tblsize: get table size
# Copyright(c) 2012 EURAC, Institute of Genetic Medicine
use strict;
use warnings;
use locale;
use Getopt::Std;
my %flags;
getopts("hxyn", \%flags);
my $SEP = ($ENV{'TBLSEP'} || "\t");
my ($file) = @ARGV;
if(!$file || defined($flags{'h'}))
{
print(STDERR qq{$0 file:
Get table width and height of a CSV file.
CSV files are TAB separated by default. You can change the column separator
by setting the TBLSEP environment variable.
-x: width only
-y: height only
-n: width and height on different lines
-h: help summary
});
exit(2);
}
my ($cols, $rows);
# fetch column count
$_ = <>;
s/[\r\n]+$//;
my @tmp = split($SEP, $_, -1);
$cols = @tmp || 1;
$rows = 1;
# row count
++$rows while(<>);
# output
if($flags{'x'}) {
print("${cols}\n");
} elsif($flags{'y'}) {
print("${rows}\n");
} elsif($flags{'n'}) {
print("${cols}\n${rows}\n");
} else {
print("${cols}x${rows}\n");
}