Skip to content

Instantly share code, notes, and snippets.

@chetan
Created April 12, 2011 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chetan/915821 to your computer and use it in GitHub Desktop.
Save chetan/915821 to your computer and use it in GitHub Desktop.
csv_grep.pl
#!/usr/bin/env perl
# csv_grep.pl
# (c) 2011, Chetan Sarva <chetan@evidon.com>
use Getopt::Long;
use Data::Dumper;
my($dump, $field, $exact, $str);
my $ret = GetOptions(
"d|dump" => \$dump,
"e|exact" => \$exact,
"f|field=i" => \$field,
);
$str = "@ARGV";
if (!$dump && (length $str == 0 || !$field)) {
print "usage: csv_grep.pl [--dump] [--exact] --field <num> <search string>\n";
exit;
}
while (<STDIN>) {
chomp;
my @s = split(/\t/);
if ($dump) {
for (my $i = 0; $i < scalar @s; $i++) {
print "$i: $s[$i]\n";
}
exit;
}
my $v = $s[$field];
if (($exact && $v eq $str) || $v =~ /$str/) {
print "$_\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment