-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_headers
executable file
·71 lines (51 loc) · 1.57 KB
/
list_headers
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
#!/usr/bin/env perl -w
use strict;
use Getopt::Long;
use Text::CSV;
use FileHandle;
use Pod::Usage;
my $LINES = undef;
my $IN_SEPARATOR = ',';
my $MAX_LENGTH = 0;
my ($HELP, $MAN);
GetOptions(
'help|?' => \$HELP,
'man' => \$MAN,
'lines' => \$LINES,
'max-length' => \$MAX_LENGTH,
'in-separator=s' => \$IN_SEPARATOR
);
pod2usage(-exitstatus => 0, -verbose => 1) if $HELP;
pod2usage(-exitstatus => 0, -verbose => 2) if $MAN;
my $CSV = Text::CSV->new({ 'binary' => 1, 'sep_char' => $IN_SEPARATOR });
my $fh = @ARGV ? new FileHandle($ARGV[0], 'r') : *STDIN;
my @fields = @{ $CSV->getline($fh) };
my $count = 0;
my %max_len = ();
if ($MAX_LENGTH) {
$CSV->column_names(\@fields);
while (my $hash = $CSV->getline_hr($fh)) {
foreach my $k (keys %$hash) {
my $l = length $hash->{$k};
$max_len{$k} = $l if !defined $max_len{$k} ||$l > $max_len{$k};
}
}
}
foreach (@fields) {
my $pfx = $LINES ? $count++ . "\t" : "";
my $app = $MAX_LENGTH ? "\t" . $max_len{$_} : "";
print "$pfx$_$app\n";
}
__END__
=head1 NAME
list_headers - prints fields in the first line of given CSV file line by line
=head1 SYNOPSIS
list_headers [options] file.csv
list_headers [options] < file.csv
=head1 OPTIONS
--help, -h short help screen
--man slightly more detailed documentation
--max-lenth print maximal length for each column
--in-separator=char field separator in the source file ("," by default)
=head1 AUTHORS
Pavel Kolesnikov <pavel@gooddata.com>