#!/bin/csh -f

# Extract columns headed by arguments from standard input (or with -v
# flag gets everything but those columns).  Assumes headings are flush
# right with column contents and headings are separated by spaces
# only.

set tmpfile = /tmp/excol.$USER.$$
set tmpfile2 = /tmp/excol.2.$USER.$$

set complement = ""
if ($#argv > 0) then
  if ("$argv[1]" == "-v") then
    set complement = "-v"
    shift
  endif
endif

if ($#argv < 1) then
  echo "Usage: excol [-v] colname ..."
  exit 1
endif

cat > $tmpfile

if ("$complement" == "") then
  set colchars
  foreach colname ($*)
    set colchars = ($colchars `cat $tmpfile | findcol "$colname"`)
  end
  cat $tmpfile | exchars $colchars > $tmpfile2
else
  foreach colname ($*)
    set colchars = (`cat $tmpfile | findcol -v "$colname"`)
    cat $tmpfile | exchars $colchars > $tmpfile2
    cat $tmpfile2 > $tmpfile
  end
endif

cat $tmpfile2

\rm -r $tmpfile
\rm -r $tmpfile2
