-
Notifications
You must be signed in to change notification settings - Fork 6
/
tuple2table
executable file
·100 lines (76 loc) · 2.11 KB
/
tuple2table
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
#!/usr/bin/perl
use utf8;
use strict;
use Data::Dumper;
use List::Util qw[min max];
# This turns a set of csv formated tuples into a formated table
# The table is formatted similar to how sql tools export a
# table of results, so if it is a large resultset you can
# further pipe it into the table-page
#
# eg input
#
# foo,bar,1
# foo,tie,2
# bar,tie,7
#
# Output:
#
# - | bar | tie
# ----+-----+-----
# foo | 1 | 2
# bar | | 7
#
# Some crazy recipes:
#
# This lists all the submodule tracking branches against all the catalyst upstreams
#
# ls | grep catalyst.moo | xargs -n1 -I % sh -i -c 'cd %; git submodule foreach --recursive "branch=\$(git config -f \$toplevel/.gitmodules submodule.\$name.branch); echo \"\$name,%,\$branch \"; "; ' 2>&1 | grep -v Enter | grep -v '/var/www' | sed 's/catalyst\.moodle\./MDL/g' | sed 's/.local//g' | tuple2table
my %data; # Contains an array of hashes of data
my %cols; # Contains an hash of column names
my $delim = ','; # option to change
my $col0width = 5;
foreach my $line (<>){
chop $line;
my ($col, $key, $val) = split $delim, $line;
$col =~ s/^\s+|\s+$//g;
$key =~ s/^\s+|\s+$//g;
$val =~ s/^\s+|\s+$//g;
my $length = length($val);
# print " $val => $length \n";
if (!$cols{$key}) {
$cols{$key} = max($length, length($key));
} else {
$cols{$key} = max($length, $cols{$key});
}
$col0width = max(length($col), $col0width);
if (!$data{$col}) {
$data{$col} = {};
}
$data{$col}{$key} = $val;
}
#print Dumper(\%data);
# print Dumper(\%cols);
#print "=========\n";
my @rows = sort keys %data;
my @cols = sort keys %cols;
# Print header row
printf " %-$col0width\s ", "Key";
foreach my $col (@cols){
printf (("| %".$cols{$col}."s "), $col);
}
print "\n";
# Print header line --+---+---
printf "-%-$col0width\s-", '-' x $col0width;
foreach my $col (@cols){
printf "+-%s-", '-' x $cols{$col};
}
print "\n";
# Print data rows
foreach my $row (@rows){
printf " %-$col0width\s ", $row;
foreach my $col (@cols){
printf (("| %".$cols{$col}."s "), $data{$row}{$col});
}
print "\n";
}