#!/usr/local/bin/perl
#
# Usage: filt <column #> <perlexpr>
#
#   Input is two or more columns of data.
#   Expr tests value in specified column.
#   If expr is true, then line is printed (passed through).
#

$usage = "Usage: filt <column #> <perlexpr>\n       (Value in column # is referred to as \$_ in your perl expression)\n";
(@ARGV == 2) || die $usage;

$n_col = shift @ARGV;
$n_col -= 1;                        # convert to zero-based index
$op = shift @ARGV;
die $usage if $op !~ m/\$_/;

$test = '$ok = ' . "( $op )";

while ( $line = <STDIN> ) {
  @col = split ' ', $line;
  $_ = $col[$n_col];
  eval $test;
  die $@ if $@;
  if ( $ok ) { print $line; }
}
