#!/bin/bash

# expar  extacts parameters from pipe
# expar [-c c1,c2,..] {-t} [-n NLINE] {-h}  (from pipe)
# -c choice of column (in the style of "cut")
# -t FS="\t", over-riding default of " "
# -n NLINE oprerate on line NLINE [1]

#-----------------------------------------------------------------------
HELP=; NLINE=1; DFS=" "; TAB=;
TF="OUT_expar.tmp"
# trap "[ -e $TF ] && rm $TF" EXIT
#-----------------------------------------------------------------------

while getopts c:tn:h OPTVAL
do 
    case $OPTVAL in
	c) COLS=$OPTARG;;
	t) DFS=;; 
	n) NLINE=$OPTARG;;
        h) HELP=1;;
        *) echo "expar -h for help"; exit -1;;
    esac
done
shift $((OPTIND-1))

if [ $HELP ]; then
  echo -e "\n expar -c c1,c2,..  {-h} parm_x parm_y .. (from pipe)"
  echo -e " primary use: extract ra & dec from pipe & pass to next task"
  echo -e " -c c1-c2 selects columns c1..c2 [1,2]; identical to Unix cut"
  echo -e ' -t FS="\t" instead of default " "'
  echo -e " -n NLINE,  operate only one line NLINE [1]"
  exit
fi


if [ $# -gt 0 ]; then
  echo "expar: this program does not accept position parameters $*"
  exit -1
fi

if [ -p /dev/stdin ]; then
      set -- "/dev/stdin"                     #set $1=/dev/stdin
	IF=$1
else
    echo "expar: no inputs from pipe"
    exit -1
fi

! [ $COLS ] && COLS="1,2"

if [ "$DFS" ]; then 
  sed -n "$NLINE"p $IF | cut -d " " -f $COLS 
else
    sed -n "$NLINE"p $IF | cut -f $COLS
fi