Debian Codegraph @

bin/tablify

raw
1 #!perl
2
3 use strict;
4 use warnings;
5 use version;
6 use File::Basename;
7 use Getopt::Long;
8 use List::Util 'max';
9 use Pod::Usage;
10 use Readonly;
11 use Text::Autoformat 'autoformat';
12 use Text::RecordParser;
13 use Text::TabularDisplay;
14
15 Readonly our $VERSION => 1.15;
16 Readonly my $DASH => q{-};
17 Readonly my $EMPTY_STR => q{};
18 Readonly my $TAB => qq{\t};
19 Readonly my $NEWLINE => qq{\n};
20 Readonly my $WIDTH => 78;
21
22 my $comment_start = '';
23 my $fs = $TAB;
24 my $rs = $NEWLINE;
25 my $headers = '';
26 my $no_headers = 0;
27 my $show_vertically = 0;
28 my $no_pager = 0;
29 my $strip_quotes = 0;
30
31 my ( $show_fields, $list, $limit, @where, $help, $man_page, $show_version );
32 GetOptions(
33 'c|comment:s' => \$comment_start,
34 'fs:s' => \$fs,
35 'rs:s' => \$rs,
36 'f|fields:s' => \$show_fields,
37 'l|list' => \$list,
38 'i|limit:i' => \$limit,
39 'w|where:s' => \@where,
40 'v|vertical' => \$show_vertically,
41 'strip-quotes' => \$strip_quotes,
42 'n|no-headers' => \$no_headers,
43 'h|headers:s' => \$headers,
44 'no-pager' => \$no_pager,
45 'help' => \$help,
46 'man' => \$man_page,
47 'version' => \$show_version,
48 ) or pod2usage;
49
50 if ( $help || $man_page ) {
51 pod2usage({
52 -exitval => 0,
53 -verbose => $man_page ? 2 : 1
54 });
55 };
56
57 if ( $show_version ) {
58 my $prog = basename( $0 );
59 print "$prog $VERSION\n";
60 exit 0;
61 }
62
63 my $file = shift or pod2usage('No file');
64 my $p = Text::RecordParser->new(
65 field_separator => $fs,
66 record_separator => $rs,
67 comment => $comment_start ? qr/^$comment_start/ : undef,
68 );
69
70 if ( $strip_quotes ) {
71 $p->field_filter( sub { s/^["']//; s/["']$//; $_ } );
72 }
73
74 if ( $file eq $DASH ) {
75 $p->fh( \*STDIN );
76 }
77 else {
78 $p->filename( $file );
79 }
80
81 my @fields;
82 if ( @fields = split( /\s*,\s*/, $headers ) ) {
83 $p->bind_fields( @fields );
84 }
85 elsif ( !$no_headers ) {
86 $p->bind_header;
87 @fields = $p->field_list;
88 }
89
90 if ( $list ) {
91 pod2usage(q[Can't list fields with --no-headers]) if $no_headers;
92 my $tab = Text::TabularDisplay->new('Field No.', 'Field');
93 my $i = 1;
94 $tab->add( $i++, $_ ) for @fields;
95 print $tab->render, $NEWLINE;
96 exit 0;
97 }
98
99 my %where;
100 for ( @where ) {
101 if ( /([\w\d]+)\s*(==|eq|>=?|<=?|=~)\s*(.*)/ ) {
102 my $field = $1;
103 my $op = $2;
104 my $value = $3;
105
106 unless ( $no_headers ) {
107 my %available = map { $_, 1 } @fields;
108 next unless $available{ $field };
109 }
110
111 $field-- if $field =~ /^\d+$/;
112 $where{ $field } = [ $op, $value ];
113 }
114 }
115
116 if ( $show_fields ) {
117 my @show = map { $_ =~ m/^\d+$/ && @fields
118 ? $_-1 < scalar @fields ? $_ : () : $_ }
119 map { $_ =~ m/(\d+)-(\d+)/ ? ( $1..$2 ) : $_ }
120 split /,/, $show_fields;
121 my @numbers = grep { /^(\d+)$/ } @show;
122
123 if ( $no_headers ) {
124 @fields = @show;
125 }
126 else {
127 if ( scalar @show == scalar @numbers ) { # all numbers
128 @numbers = map { $_ - 1 } @numbers;
129 @fields = @fields[ @numbers ];
130 }
131 elsif ( @show ) {
132 my %available = map { $_, 1 } @fields;
133 my @temp = @fields;
134 @fields = map { $available{ $_ } ? $_ : () } @show;
135 }
136
137 if ( !@fields ) {
138 die "No fields match in list '$show_fields'\n";
139 }
140 }
141 }
142
143 my $fh;
144 my $pager = $ENV{'PAGER'};
145 if ( !$no_pager && $pager ) {
146 open $fh, "| $pager";
147 }
148 else {
149 $fh = \*STDOUT;
150 }
151
152 my $tab = Text::TabularDisplay->new( @fields );
153
154 my $max_col_length = 0;
155 my $num_records = 0;
156 my $separator = "************ Record %s ************\n";
157 if ( $no_headers ) {
158 my @field_names;
159 RECORD:
160 while ( my @data = $p->fetchrow_array ) {
161 if ( !@fields ) {
162 @fields = ( 0..$#data );
163 @field_names = map { 'Field' . ($_+1) } @fields;
164 $max_col_length = max( map { length $_ } @field_names );
165 $tab->columns( @field_names );
166 }
167
168 for my $field ( keys %where ) {
169 my ( $op, $value ) = @{ $where{ $field } };
170 my $cmd = "'$data[ ($field - 1) ]' $op $value";
171 next RECORD unless eval $cmd;
172 }
173
174 $num_records++;
175 if ( $show_vertically ) {
176 printf $fh $separator, $num_records;
177 for my $i ( @fields ) {
178 printf $fh "%${max_col_length}s: %s\n",
179 $field_names[ $i ],
180 defined $data[ $i ] ? $data[ $i ] : q{};
181 }
182 }
183 else {
184 $tab->add( map { $data[ $_ ] } @fields );
185 }
186
187 last if $limit && $num_records >= $limit;
188 }
189 }
190 else {
191 $max_col_length = max map { $_ ? length $_ : 0 } $p->field_list;
192
193 RECORD:
194 while ( my $data = $p->fetchrow_hashref ) {
195 for my $field ( keys %where ) {
196 my ( $op, $value ) = @{ $where{ $field } };
197 my $cmd = "'$data->{ $field }' $op $value";
198 next RECORD unless eval $cmd;
199 }
200
201 $num_records++;
202 if ( $show_vertically ) {
203 printf $fh $separator, $num_records;
204 for my $field ( @fields ) {
205 next unless $field;
206 my $v = defined $data->{ $field } ? $data->{ $field } : q{};
207 if ( length $v > $WIDTH ) {
208 ( $v = autoformat( $v, { left => $max_col_length + 3 } ) )
209 =~ s/^\s+|\s+$//g;
210 }
211
212 printf $fh "%${max_col_length}s: %s\n", $field, $v;
213 }
214 }
215 else {
216 $tab->add( map { $data->{ $_ } } @fields );
217 }
218 last if $limit && $num_records >= $limit;
219 }
220 }
221
222 if ( !$show_vertically ) {
223 print $fh $tab->render;
224 }
225
226 print $fh $num_records
227 ? sprintf(
228 "\n%s record%s returned\n", $num_records, $num_records > 1
229 ? 's' : $EMPTY_STR
230 )
231 : "\nNo records returned\n";
232
233 close $fh;
234
235 __END__
236
237 # -------------------------------------------------------------------
238
239 # $Id: tablify,v 1.14 2006/03/07 17:20:00 kclark Exp $
240
241 =pod
242
243 =head1 NAME
244
245 tablify - turn a delimited text file into a text table
246
247 =head1 SYNOPSIS
248
249 tablify [options] file
250
251 Options:
252
253 -h|--help Show help
254 -c|--comment Define the beginning of a (single-line) comment
255 -n|--no-headers Assume first line is data, not headers
256 --no-pager Do not use $ENV{'PAGER'} even if defined
257 --strip-quotes Strip " or ' around fields
258 -l|--list List the fields in the file (for use with -f)
259 -f|--fields=f1[,f2] Show only fields in comma-separated list;
260 when used in conjunction with "no-headers"
261 the list should be field numbers (starting at 1);
262 otherwise, should be field names
263 -w|where=f<cmp>v Apply the "cmp" Perl operator to restrict output
264 where field "f" matches the value "v"; acceptable
265 operators include ==, eq, >, >=, <=, and =~
266 -v|--vertical Show records vertically
267 -i|--limit=n Limit to given number of records
268 --fs=x Use "x" as the field separator
269 (default is tab "\t")
270 --rs=x Use "x" as the record separator
271 (default is newline "\n")
272 --as-html Create an HTML table instead of plain text
273 --headers Comma-separated list of names matching
274 the number of columns
275
276 =head1 DESCRIPTION
277
278 This script is essentially a quick way to parse a delimited text file
279 and view it as a nice ASCII table. By selecting only certain B<fields>,
280 employing a B<where> clause to only select records where a field matches
281 some value, and using the B<limit> to only see some of the output, you
282 almost have a mini-database front-end for a simple text file.
283
284 =head1 EXAMPLES
285
286 Given a data file like this:
287
288 name,rank,serial_no,is_living,age
289 George,General,190293,0,64
290 Dwight,General,908348,0,75
291 Attila,Hun,,0,56
292 Tojo,Emporor,,0,87
293 Tommy,General,998110,1,54
294
295 To find the fields you can reference, use the B<list> option:
296
297 $ tablify --fs ',' -l people.dat
298 +-----------+-----------+
299 | Field No. | Field |
300 +-----------+-----------+
301 | 1 | name |
302 | 2 | rank |
303 | 3 | serial_no |
304 | 4 | is_living |
305 | 5 | age |
306 +-----------+-----------+
307
308 To extract just the name and serial numbers, use the B<fields> option:
309
310 $ tablify --fs ',' -f name,serial_no people.dat
311 +--------+-----------+
312 | name | serial_no |
313 +--------+-----------+
314 | George | 190293 |
315 | Dwight | 908348 |
316 | Attila | |
317 | Tojo | |
318 | Tommy | 998110 |
319 +--------+-----------+
320 5 records returned
321
322 To extract the first through third fields and the fifth field (where
323 field numbers start at "1" -- tip: use the B<list> option to quickly
324 determine field numbers), use this syntax for B<fields>:
325
326 $ tablify --fs ',' -f 1-3,5 people.dat
327 +--------+---------+-----------+------+
328 | name | rank | serial_no | age |
329 +--------+---------+-----------+------+
330 | George | General | 190293 | 64 |
331 | Dwight | General | 908348 | 75 |
332 | Attila | Hun | | 56 |
333 | Tojo | Emporor | | 87 |
334 | Tommy | General | 998110 | 54 |
335 +--------+---------+-----------+------+
336 5 records returned
337
338 To select only the ones with six serial numbers, use a B<where>
339 clause:
340
341 $ tablify --fs ',' -w 'serial_no=~/^\d{6}$/' people.dat
342 +--------+---------+-----------+-----------+------+
343 | name | rank | serial_no | is_living | age |
344 +--------+---------+-----------+-----------+------+
345 | George | General | 190293 | 0 | 64 |
346 | Dwight | General | 908348 | 0 | 75 |
347 | Tommy | General | 998110 | 1 | 54 |
348 +--------+---------+-----------+-----------+------+
349 3 records returned
350
351 To find Dwight's record, you would do this:
352
353 $ tablify --fs ',' -w 'name eq "Dwight"' people.dat
354 +--------+---------+-----------+-----------+------+
355 | name | rank | serial_no | is_living | age |
356 +--------+---------+-----------+-----------+------+
357 | Dwight | General | 908348 | 0 | 75 |
358 +--------+---------+-----------+-----------+------+
359 1 record returned
360
361 To find the name of all the people with a serial number who are living:
362
363 $ tablify --fs ',' -f name -w 'is_living==1' -w 'serial_no>0' people.dat
364 +-------+
365 | name |
366 +-------+
367 | Tommy |
368 +-------+
369 1 record returned
370
371 To filter outside of program and simply format the results, use "-" as
372 the last argument to force reading of STDIN (and probably assume no
373 headers):
374
375 $ grep General people.dat | tablify --fs ',' -f 1-3 --no-headers -
376 +---------+--------+--------+
377 | Field1 | Field2 | Field3 |
378 +---------+--------+--------+
379 | General | 190293 | 0 |
380 | General | 908348 | 0 |
381 | General | 998110 | 1 |
382 +---------+--------+--------+
383 3 records returned
384
385 When dealing with data lacking field names, you can specify "no-headers"
386 and then refer to fields by number (starting at one), e.g.:
387
388 $ tail -5 people.dat | tablify --fs ',' --no-headers -w '3 eq "General"' -
389 +--------+---------+--------+--------+--------+
390 | Field1 | Field2 | Field3 | Field4 | Field5 |
391 +--------+---------+--------+--------+--------+
392 | George | General | 190293 | 0 | 64 |
393 | Dwight | General | 908348 | 0 | 75 |
394 | Tommy | General | 998110 | 1 | 54 |
395 +--------+---------+--------+--------+--------+
396 3 records returned
397
398 If your file has many fields which are hard to see across the screen,
399 consider using the vertical display with "-v" or "--vertical", e.g.:
400
401 $ tablify --fs ',' -v --limit 1 people.dat
402 ************ Record 1 ************
403 name: George
404 rank: General
405 serial_no: 190293
406 is_living: 0
407 age : 64
408
409 1 record returned
410
411 =head1 SEE ALSO
412
413 =over 4
414
415 =item * Text::RecordParser
416
417 =item * Text::TabularDisplay
418
419 =item * DBD::CSV
420
421 Although I don't DBD::CSV this module, the idea was much the inspiration
422 for this. I just didn't want to have to install DBI and DBD::CSV to
423 get this kind of functionality. I think my interface is simpler.
424
425 =back
426
427 =head1 AUTHOR
428
429 Ken Youens-Clark E<lt>[email protected]<gt>.
430
431 =head1 LICENSE AND COPYRIGHT
432
433 Copyright (C) 2006-10 Ken Youens-Clark. All rights reserved.
434
435 This program is free software; you can redistribute it and/or modify
436 it under the terms of the GNU General Public License as published by
437 the Free Software Foundation; version 2.
438
439 This program is distributed in the hope that it will be useful, but
440 WITHOUT ANY WARRANTY; without even the implied warranty of
441 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
442 General Public License for more details.
443
444 =cut