#!/afs/athena/contrib/perl/perl $RCS_ID = '$Id: sorttbl,v 2.1 1992/06/10 15:30:48 hobbs Exp $' ; $0 =~ s-.*/-- ; $HelpInfo = <){ if( $lln < 2 ){ print unless $CHK ; if( /^#\s|^\s+#/ && ! $lln ){ next ; } # comment chop ; @H = split( /\t/, $_ ); if( ++$lln == 1 ){ # header line @F = @H ; &get_col_x ; } # chk column names else{ # definition line $| = 0 ; for $arg ( @ARGV ){ if( $arg =~ /^-r.*/ ){ $cf .= 'r' ; next ; } for( $f=0, $g=1 ; $f <= $#F ; $f++, $g++ ){ if( $arg eq $F[$f] ){ if( $H[$f] =~ /(\S+)/ && $1 =~ /N/i ){ # numeric $cf .= 'n' ; } if( $H[$f] =~ /(\S+)/ && $1 =~ /M/i ){ # month $cf .= 'M' ; } $sarg .= "+$f -$g$cf " ; $cf = '' ; last ; } } } if( $CHK && $sarg =~ /\+/ ){ $ADDCOL++ ; $f = @H ; $g = $f +1 ; $cf = 'n' ; $sarg .= "+$f -$g$cf " ; } print "sort arg: -t\"\t\" $sarg\n" if $XBUG ; open( SS, "| sort -t\"\t\" $sarg" ) || # UNIX sort die "Can't open pipe to sort\n" ; } next ; } if( $ADDCOL ){ # add dummy column for check option chop ; print SS $_, "\t", $., "\n" ; } else{ # normal case print SS $_ ; } } close( SS ) ; sub get_col_x { # get, chk column indexes, inc -v, die if bad column # uses @H, $INV, put indexes in @n. # modified for sorttbl. local( $f, $ok, @nn ) ; for $arg (@ARGV){ next if $arg =~ /^-/ ; for( $ok=$f=0 ; $f < @H ; $f++ ){ if( $arg eq $H[$f] ){ # match existing column $ok++ ; push( @n, $f ); last ; } } die "$0: Bad column name: $arg\n" if ! $ok ; } if( $INV ){ # inverse option loop: for( $f=0 ; $f < @H ; $f++ ){ for $i (@n){ next loop if $i eq $f ; } push( @nn, $f ); } @n = @nn ; } }