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.
62 use Scalar::Util qw(looks_like_number);
66 my %options = (debug => 0,
79 'has_header|has-header!',
80 'debug|d+','help|h|?','man|m');
82 pod2usage() if $options{help};
83 pod2usage({verbose=>2}) if $options{man};
85 $DEBUG = $options{debug};
89 # push @USAGE_ERRORS,"You must pass something";
92 if (not @{$options{field}}) {
93 $options{field} = [1];
98 if ($options{has_header}) {
99 # we allow for a header
102 my @field_indexes = map { looks_like_number($_) && $_ > 0 ? $_ - 1 : $_;}
103 map {split /,/} @{$options{field}};
105 @field_indexes{@field_indexes} = @field_indexes;
106 @field_indexes = values %field_indexes;
108 if (grep {not /-?\d+/} @field_indexes and not $options{has_header}) {
109 push @USAGE_ERRORS,"Invalid field index(es)";
112 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
114 if (not @{$options{regexp}} and @ARGV) {
115 push @{$options{regexp}}, shift @ARGV;
119 # we'll use this as a special indicator to read stdin
122 my %compiled_regexes;
123 my $csv = Text::CSV->new({sep_char=>$options{delimiter}});
124 FILE: for my $file (@ARGV) {
126 my $headers_updated = 0;
127 if (not defined $file) {
131 $fh = IO::File->new($file,'r') or
132 die "Unable to open $file for reading: $!";
134 LINE: while (<$fh>) {
137 die "Unable to parse line $. of $file: ".$csv->error_diag() unless $csv->parse($_);
138 my @fields = $csv->fields();
139 # skip lines which don't have enough fields
140 if ($options{has_header} and not @header) {
142 @header{@header} = 0..$#fields;
144 print $/ if $chomped;
147 if ($options{has_header} and not $headers_updated) {
148 $headers_updated = 0;
149 if (@header < @fields) {
150 @header{@header} = 1..@fields;
153 for my $index (@field_indexes) {
154 push @new_indexes,$index and next if $index =~ /^-?\d+$/;
155 if (not exists $header{$index}) {
157 print STDERR Dumper(\%header);
158 print STDERR "Invalid header $index\n";
161 push @new_indexes,$header{$index};
164 @field_indexes = @new_indexes;
166 next LINE if grep {not defined $_} @fields[@field_indexes];
167 REGEX: for my $regex (@{$options{regexp}}) {
168 FIELDS: for my $field (@fields[@field_indexes]) {
170 if (not exists $compiled_regexes{$regex}) {
171 $compiled_regexes{$regex} = qr/$regex/;
173 $field =~ $compiled_regexes{$regex} or next LINE;
178 print $/ if $chomped;