7 td-select - Show only the specified columns from the input tabular data stream.
11 td-select [I<OPTIONS>] [--] [-]I<COLUMN> [[-]I<COLUMN> [...]]
17 =item -H, --no--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
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.
44 I<COLUMN> is either a column name,
45 or one of these special keywords:
55 the rest of columns not given yet in the parameter list
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:
73 ls -l | td-trans-ls | td-select -- NAME +REST -INODE -LINKS -MAJOR -MINOR
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.
83 $OptWarnBadColumnNames = 1;
84 $OptFailBadColumnNames = 1;
85 $OptFailBadNegativeColumnNames = 0;
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
>);
103 my $arg = shift @ARGV;
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 =~ /^(.+)$/)
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;
141 die "$0: unknown parameter: $arg\n";
145 # display selected headers
146 if($OptShowHeader and @Columns)
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
160 my @Input = read_record
(\
*STDIN
);
164 for my $col (@Columns)
166 push @Output, defined $Header{$col} ?
$Input[$Header{$col}] : '';
169 print join($FS, @Output).$RS;