| 1 |
#!/usr/bin/perl -l |
| 2 |
|
| 3 |
use strict; |
| 4 |
use vars qw($VERSION); |
| 5 |
use vars qw($t $data @data $re); |
| 6 |
use vars qw(@f $help $separator); |
| 7 |
|
| 8 |
$VERSION = "1.00"; |
| 9 |
|
| 10 |
use Getopt::Long; |
| 11 |
use Text::TabularDisplay; |
| 12 |
|
| 13 |
GetOptions("f=s" => \@f, |
| 14 |
"h!" => \$help, |
| 15 |
"s=s" => \$separator); |
| 16 |
|
| 17 |
if (defined $help) { |
| 18 |
print usage(); |
| 19 |
exit 0; |
| 20 |
} |
| 21 |
|
| 22 |
$separator = '\s' unless defined $separator; |
| 23 |
$re = qr($separator); |
| 24 |
|
| 25 |
$t = Text::TabularDisplay->new; |
| 26 |
|
| 27 |
chomp($data = <STDIN>); |
| 28 |
|
| 29 |
@data = split $re, $data; |
| 30 |
|
| 31 |
@f = split /,/, $f[0] if (@f == 1); |
| 32 |
@f = (0 .. $#data) unless (@f); |
| 33 |
|
| 34 |
$t->columns(@data[@f]); |
| 35 |
|
| 36 |
while (defined($data = <STDIN>)) { |
| 37 |
chomp $data; |
| 38 |
@data = split $re, $data; |
| 39 |
$t->add(@data[@f]) |
| 40 |
} |
| 41 |
|
| 42 |
print $t->render; |
| 43 |
|
| 44 |
sub usage { |
| 45 |
require File::Basename; |
| 46 |
my $prog = File::Basename::basename($0); |
| 47 |
return <<"USAGE"; |
| 48 |
$prog v.$VERSION |
| 49 |
$prog [-s \$separator] [-f fields] < data |
| 50 |
|
| 51 |
data should be a series of \$separator-delimited lines, |
| 52 |
which will be displayed in a table, of which the fields |
| 53 |
defined by -f will be displayed (defaults to all fields). |
| 54 |
|
| 55 |
USAGE |
| 56 |
} |