tabularize auto-pager
[hband-tools.git] / tabdata / td-select
blobb3c3ce314151990d6acd1b52eb20662e30453b4a
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 td-select - Show only the specified columns from the input tabular data stream.
9 =head1 USAGE
11 td-select [I<OPTIONS>] [--] [-]I<COLUMN> [[-]I<COLUMN> [...]]
13 =head1 OPTIONS
15 =over 4
17 =item -H, --no--header
19 do not show headers
21 =item -h, --header
23 show headers (default)
25 =item -i, --ignore-non-existing-columns
27 do not treat non-existing (missing or typo) column names as failure
29 =item -w, --warn-non-existing-columns
31 only show warning on non-existing (missing or typo) column names, but
32 don't fail
34 =item --strict-columns
36 warn and fail on non-existing (missing or typo) column names given in
37 parameters, even if it's prefixed with hyphen, ie. when the user want to
38 remove the named column from the output.
40 =back
42 =head1 DESCRIPTION
44 I<COLUMN> is either a column name,
45 or one of these special keywords:
47 =over 4
49 =item +ALL
51 all columns
53 =item +REST
55 the rest of columns not given yet in the parameter list
57 =back
59 I<COLUMN> is optionally prefixed with minus (C<->),
60 in which case the given column will not be shown,
61 ie. removed from the shown columns.
63 So if you want to show all columns except one or two:
65 td-select +ALL -PASSWD
67 If you want to put a given column (say "KEY") to the first place and left others intact:
69 td-select KEY +REST
71 =head1 EXAMPLE
73 ls -l | td-trans-ls | td-select -- NAME +REST -INODE -LINKS -MAJOR -MINOR
75 =head1 REFERENCES
77 "Select" in td-select comes from SQL.
78 Similarly to SQL, td-select(1) is to choose some of the columns and return them in the given order.
80 =cut
82 $OptShowHeader = 1;
83 $OptWarnBadColumnNames = 1;
84 $OptFailBadColumnNames = 1;
85 $OptFailBadNegativeColumnNames = 0;
86 %OptionDefs = (
87 'h|header' => sub { $OptShowHeader = 1; },
88 'H|no-header' => sub { $OptShowHeader = 0; },
89 'i|ignore-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 0; },
90 'w|warn-non-existing-columns' => sub { $OptFailBadColumnNames = 0; $OptWarnBadColumnNames = 1; },
91 'strict-columns' => sub { $OptWarnBadColumnNames = 1; $OptFailBadColumnNames = 1; $OptFailBadNegativeColumnNames = 1; },
94 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
95 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
97 process_header(scalar <STDIN>);
99 @Columns = ();
101 while(@ARGV)
103 my $arg = shift @ARGV;
105 if($arg =~ /^--/)
107 die "$0: unknown parameter: $arg\n";
109 elsif($arg =~ /^-(.+)$/)
111 my $except_column = $1;
112 if(not exists $Header{$except_column})
114 my $cols = join ", ", @Header;
115 warn "$0: $except_column: no such column. known columns: $cols\n" if $OptWarnBadColumnNames;
116 exit 3 if $OptFailBadNegativeColumnNames;
118 @Columns = grep {$_ ne $except_column} @Columns;
120 elsif($arg eq '+ALL')
122 push @Columns, @Header;
124 elsif($arg eq '+REST')
126 push @Columns, grep {not $_ ~~ @Columns} @Header;
128 elsif($arg =~ /^(.+)$/)
130 my $colname = $1;
131 if(not exists $Header{$colname})
133 my $cols = join ", ", @Header;
134 warn "$0: $colname: no such column. known columns: $cols\n" if $OptWarnBadColumnNames;
135 exit 3 if $OptFailBadColumnNames;
137 push @Columns, $colname;
139 else
141 die "$0: unknown parameter: $arg\n";
145 # display selected headers
146 if($OptShowHeader and @Columns)
148 @Output = ();
149 for my $col (@Columns)
151 push @Output, defined $Header{$col} ? $Header[$Header{$col}] : '';
153 print join($FS, @Output).$RS;
157 # display selected data fields
158 while(not eof STDIN)
160 my @Input = read_record(\*STDIN);
162 @Output = ();
164 for my $col (@Columns)
166 push @Output, defined $Header{$col} ? $Input[$Header{$col}] : '';
169 print join($FS, @Output).$RS;