2 # col_grep greps a column matching a pattern, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1153 2008-04-08 00:04:20Z don $
17 col_grep - grep for a column matching a pattern
21 col_grep [options] PATTERN [FILE...]
22 col_grep [options] [-e PATTERN] [FILE...]
25 --delimiter, -d Delimiter to use (default tab)
26 --regexp, -e pattern to match
27 --fields, -f field number to match (default 1)
28 --help, -h display this help
29 --man, -m display manual
35 =item B<--delimiter,-d>
37 Delimiter to use, defaults to tab
45 Field number to match; starts at 1
49 Display brief useage information.
65 my %options = (debug => 0,
77 'debug|d+','help|h|?','man|m');
79 pod2usage() if $options{help};
80 pod2usage({verbose=>2}) if $options{man};
82 $DEBUG = $options{debug};
86 # push @USAGE_ERRORS,"You must pass something";
89 if (not @{$options{field}}) {
90 $options{field} = [1];
93 my @field_indexes = map { $_ > 0 ? $_ - 1 : $_;}
94 map {split /,/} @{$options{field}};
96 @field_indexes{@field_indexes} = @field_indexes;
97 @field_indexes = values %field_indexes;
99 if (grep {not /-?\d+/} @field_indexes) {
100 push @USAGE_ERRORS,"Invalid field index(es)";
103 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
105 if (not @{$options{regexp}} and @ARGV) {
106 push @{$options{regexp}}, shift @ARGV;
109 my %compiled_regexes;
113 my @fields = split /$options{delimiter}/;
114 # skip lines which don't have enough fields
115 next LINE if grep {not defined $_} @fields[@field_indexes];
116 REGEX: for my $regex (@{$options{regexp}}) {
117 FIELDS: for my $field (@fields[@field_indexes]) {
119 if (not exists $compiled_regexes{$regex}) {
120 $compiled_regexes{$regex} = qr/$regex/;
122 $field =~ $compiled_regexes{$regex} or next LINE;
127 print $/ if $chomped;