-
Notifications
You must be signed in to change notification settings - Fork 34
/
ascii_table_csv.pl
executable file
·107 lines (82 loc) · 2.27 KB
/
ascii_table_csv.pl
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 15 March 2013
# https://github.com/trizen
# Print a CSV file to standard output as an ASCII table.
use 5.010;
use strict;
use autodie;
use warnings;
use open IO => ':utf8';
use Text::CSV qw();
use Text::ASCIITable qw();
use Getopt::Std qw(getopts);
binmode(STDOUT, ':utf8');
my %opt = (
s => 0,
d => ',',
);
getopts('sw:d:', \%opt);
my $csv_file = shift() // die <<"USAGE";
usage: $0 [options] [csv_file]
options:
-s : allow whitespace in CSV (default: $opt{s})
-d <> : separator character (default: '$opt{d}')
-w <> : maximum width for table (default: no limit)
example: $0 -s -d ';' -w 80 file.csv
USAGE
my %esc = (
a => "\a",
t => "\t",
r => "\r",
n => "\n",
e => "\e",
b => "\b",
f => "\f",
);
$opt{d} =~ s{(?<!\\)(?:\\\\)*\\([@{[keys %esc]}])}{$esc{$1}}g;
## Parse the CSV file
sub parse_file {
my ($file) = @_;
my %record;
open my $fh, '<', $file;
my $csv = Text::CSV->new(
{
binary => 1,
allow_whitespace => $opt{s},
sep_char => $opt{d},
}
)
or die "Cannot use CSV: " . Text::CSV->error_diag();
my $columns = $csv->getline($fh);
my $lines = 0;
while (my $row = $csv->getline($fh)) {
foreach my $i (0 .. $#{$columns}) {
push @{$record{$columns->[$i]}}, $row->[$i];
}
++$lines;
}
$csv->eof() or die "CSV ERROR: " . $csv->error_diag(), "\n";
close $fh;
return ($columns, \%record, $lines);
}
## Create the ASCII table
sub create_ascii_table {
my ($columns, $record, $lines) = @_;
my $table = Text::ASCIITable->new();
$table->setCols(@{$columns});
if ($opt{w}) {
foreach my $column (@{$columns}) {
$table->setColWidth($column, $opt{w} / @{$columns});
}
}
foreach my $i (0 .. $lines - 1) {
$table->addRow(map { $_->[$i] } @{$record}{@{$columns}});
}
return $table;
}
{
local $| = 1;
print create_ascii_table(parse_file($csv_file));
}