-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsvview
More file actions
executable file
·288 lines (228 loc) · 6.04 KB
/
csvview
File metadata and controls
executable file
·288 lines (228 loc) · 6.04 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env perl
# Quick way to view a csv file on the command line
use warnings;
no warnings qw{uninitialized};
use Getopt::Long;
use Encode;
my $opts = { "c|count=i" => \my $count, };
GetOptions(%$opts) or die "Usage:\n" . join( "\n", sort keys %$opts ) . "\n";
my %field_types = (
num => qr/^[\.,\d]+[%gmt]{0,1}$/i,
nnum => qr/^[\-\.,\d]+$/,
alph => qr/^[a-z]+$/i,
anum => qr/^[a-z0-9]+$/i,
msc => qr/./i,
blank => qr/^[\s]*$/,
empty => qr/^$/,
);
my @field_type_order = qw(num nnum alph anum blank empty msc);
my $file;
my $is_temp_file = 0;
if ( !-t STDIN ) {
$file = "/tmp/csvvview.stdin.$$.csv";
$is_temp_file = 1;
open( F, ">", $file ) || die $!;
while (<>) {
print F $_;
}
close(F);
}
else {
$file = shift @ARGV || die "File?";
}
open( F, $file ) || die $!;
my %stats = ( File => $file );
my %fields = ();
my $delimiter = find_delimiter();
my $screen_lines = $ENV{LINES} - 1;
my $empty_line_regex = qr/^[$delimiter\s]*$/;
my @header;
my $header;
my @column_ids_header;
my $column_ids_header;
my @line;
my $line;
analyze_data();
my $lines_shown = 0;
my $last_line;
open( F, $file ) || die $!;
binmode STDOUT, ":utf8";
while (<F>) {
s/[\r\n]//g;
# convert latin to utf8 if necessary
if (/[\xc0\xc1\xc4-\xff]/) {
$stats{encoding} = "latin1";
$_ = decode( "iso-8859-15", $_ );
}
if ( $_ =~ $empty_line_regex || $_ =~ /^#/ ) {
next;
}
if ( $lines_shown % $screen_lines == 0 ) {
print stats()
. $column_ids_header
. $header
. padded_line( \@line, "-" );
$lines_shown += 4;
next;
}
print padded_line( [ split($delimiter) ] );
$lines_shown++;
$last_line = $.;
}
print "\n" x ( $screen_lines
- ( $lines_shown - int( $lines_shown / $screen_lines ) * $screen_lines )
);
close(F);
sub find_delimiter {
my $sample;
open( F, $file ) || die $!;
my $line = 0;
while (<F>) {
next if /^#/;
s/[\r\n]//g;
$line++;
$sample .= $_;
last if $line == 3;
}
close(F);
my @special_chars = $sample =~ /([^\w"])/g;
my %special_char_count = ();
map { $special_char_count{$_}++ } @special_chars;
my $delimiter;
if ( $sample =~ / {3,}/ ) {
$delimiter = " +";
}
else {
my $max = 0;
foreach my $special_char ( keys %special_char_count ) {
next if $special_char_count{$special_char} < $max;
$delimiter = $special_char;
$max = $special_char_count{$special_char};
}
}
$stats{delimiter} = '"' . $delimiter . '"';
return $delimiter || die "No delimiter found.";
}
sub calculate_alpha_column_name {
my ($num) = @_;
my $r;
while ( $num != 0 ) {
$r = chr( $num % 26 + 64 ) . $r if $num % 26;
$num = int( $num / 26 );
}
return $r;
}
sub analyze_data {
my $header_line;
open( F, $file ) || die $!;
while (<F>) {
$stats{format} = "dos" if /\r/;
s/[\r\n]//g;
$stats{Lines} = $.;
if ( $. == 1 ) {
$header_line = $_;
next;
}
if ( $_ =~ $empty_line_regex ) {
$stats{"Empty Lines"}++;
next;
}
add_field_stats( [ split($delimiter) ] );
}
close(F);
@header = split( $delimiter, $header_line );
add_field_stats( \@header, 1 );
set_field_types();
create_column_ids_header();
add_field_stats( \@column_ids_header, 1 );
$column_ids_header = padded_line( \@column_ids_header );
$header = padded_line( \@header );
my $i = -1;
foreach my $value (@header) {
$i++;
$line[$i] = "-" x $fields{$i}{length};
}
$line = padded_line( \@line );
foreach my $field ( keys %fields ) {
if ( $fields{$field}{type} eq "blank" ) {
$stats{"Blank columns"}++;
next;
}
if ( $fields{$field}{type} eq "empty" ) {
$stats{"Empty columns"}++;
next;
}
}
}
sub add_field_stats {
my ( $line, $ignore_empty ) = @_;
my $i = -1;
foreach my $value (@$line) {
$i++;
if ( !$ignore_empty || $fields{$i}{length} ) {
$fields{$i}{length} = length($value)
if length($value) > $fields{$i}{length};
}
next if $ignore_empty;
$fields{$i}{values}{$value}++;
foreach my $field_type (@field_type_order) {
my $regex = $field_types{$field_type};
if ( $value =~ $regex ) {
$fields{$i}{types}{$field_type} = $value;
last;
}
}
}
}
sub padded_line {
my ( $in, $pad ) = @_;
$pad ||= " ";
my @out;
my $i2 = -1;
foreach my $i ( sort { $a <=> $b } keys %fields ) {
my $value = $in->[$i];
next if $fields{$i}{type} =~ /empty|blank/;
$i2++;
$fields{$i}{name} = $header[$i];
my $justify = $fields{$i}{type} eq "num" ? "" : "-";
$out[$i2]
= sprintf( '%' . $justify . $fields{$i}{length} . 's', $value );
}
return join( " | ", @out ) . "\n";
}
sub stats {
my $s = "File: " . $stats{File};
local $stats{File};
delete $stats{File};
my @r;
my $i = -1;
map { $i++; $r[$i] = $_ . ": " . $stats{$_} } sort keys %stats;
return $s . ", " . join( ", ", @r ) . "\n";
}
sub create_column_ids_header {
my $i = -1;
foreach my $value (@header) {
$i++;
$column_ids_header[$i]
= ( $i + 1 ) . " ("
. calculate_alpha_column_name( $i + 1 ) . ") "
. $fields{$i}{type} . " "
. keys( %{ $fields{$i}{values} } );
}
}
sub set_field_types {
my $i = -1;
foreach my $field (@header) {
$i++;
foreach my $type (@field_type_order) {
next if !exists $fields{$i}{types}{$type};
$fields{$i}{type} = $type;
last;
}
}
}
END {
if ($is_temp_file) {
unlink($file) if -e $file;
}
}