#!/bin/sh
# compares the sets of columns specified by -1 and -2 via sdiff (or diff).
#   optionally sorts and uniqs data.
#   leaving out -2 causes the -1 columns to be used for both
set -- `getopt "h1:2:p:q:dsu" "$@"` || {
    echo "Usage: `basename $0` [-1 cols1tokeep] [-2 cols2tokeep] file1 file2\n\t[cols1tokeep]\tlist of column numbers separated by commas, for file 1 (or both)\n\t[cols2tokeep]\tsame list for file 2" 1>&2
    exit 1
}
cols1tokeep=NONE
cols2tokeep=NONE
prefix="cat"
diff="sdiff -w 180"
sort="cat"
uniq=""
cat=NONE
while :
do
  case "$1" in
      -h) echo "Usage: `basename $0` [-1 cols1tokeep] [-2 cols2tokeep] [-s] [-u] [-d] file1 file2\n\t[cols1tokeep]\tlist of column numbers separated by commas, for file 1\n\t[cols2tokeep]\tsame list for file 2" ;;
      -1) shift; cols1tokeep="$1" ;;
      -2) shift; cols2tokeep="$1" ;;
      -p) shift; prefix="grep $1" ;;
      -q) shift; prefix="grep -v $1" ;;
      -d) diff="diff" ;;
      -s) sort="sort"; cat=off ;;
      -u) uniq=" -u"; sort="sort"; cat=off ;;
      --) break ;;
  esac
  shift
done
shift

## assemble the awk program that will pull out the columns
print1="{print"
print2="}"
if test $cols1tokeep != NONE
then
    cols="$(echo $cols1tokeep | sed 's/\([0-9]*\)/$\1 /g')"
    awkprog1="$(echo $print1 $cols $print2)"
fi
awkprog2="$awkprog1"

if test $cols1tokeep != NONE
then
    cols="$(echo $cols1tokeep | sed 's/\([0-9]*\)/$\1 /g')"
    awkprog2="$(echo $print1 $cols $print2)"
fi

## assemble the sorting options
sortoption="$(echo $sort $uniq)"

awk "$awkprog1" $1 | `echo $sortoption` | `echo $prefix` > eraseme.tmp1
awk "$awkprog2" $2 | `echo $sortoption` | `echo $prefix` > eraseme.tmp2

`echo $diff` eraseme.tmp1 eraseme.tmp2
rm eraseme.tmp1 eraseme.tmp2
