#!/bin/sh
#
# Applies arbitrary AWK expressions to fields in an rdbtable.
#
# Author: Carlo Strozzi <carlos@linux.it>

RCS_ID='$Id$'

my_name=$(basename $0)

while [ $# -ge 1 ] ; do
  case $1 in
    -h*) cat <<_EOH_

        NoSQL operator: ${my_name}

Usage:  ${my_name}  [options] [expression]

Options:
    -help   Print this help info.
    -n      Strip header from output.

Computes values for data fields based on arbitrary AWK statements of
the type 'pattern { action }', using column names. Chars that are special
to the UNIX shell must be quoted. 

Column names in the left-hand side ('pattern') of the AWK expression must
be referred to with the construct '\$P["column_name"]', while those in
the right-hand side ('{ action }') must be in the form 'P["column_name"]',
i.e. without the '\$' sign. For example, to set the JOB column to the value
"Programmer" in all rows where the NAME column value is equal to "Hobbs"
the expression is:

        '\$P["NAME"] == "Hobbs" { V[P["JOB"]] = "Programmer" }' 

which means: 'Find all rows where position ('P[]') 'NAME' is equal to
"Hobbs" and set the value ('V[]') of their column 'JOB' to the string
"Programmer".  Ordinary AWK field numbers may be used in expressions, i.e.:

             '\$(3) == "Hobbs" { V[3] = "Programmer" }'

This operator reads an rdbtable via STDIN and writes an rdbtable
via STDOUT.  If no selection expression is provided, then the whole
table is written to STDOUT.


$RCS_ID

            ----------------------
NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.
This program comes with ABSOLUTELY NO WARRANTY; for details
refer to the GNU General Public License.

You should have received a copy of the GNU General Public License
along with this program;  if not, write to the Free Software
Foundation, Inc., 59 Temple Place Suite 330, Boston, MA 02111-1307
USA.
            ----------------------

_EOH_
        exit 0
        ;;
    -n) shift; no_hdr=1 ;;
    *)  break ;;
  esac
done

awk 'BEGIN{ NULL=""; FS="\t" }
# Table comments.
r == 0 && $0 ~ /^ *#/ { if( "'${no_hdr}'" != 1 ) print; next }
# Column names and positions.
r == 0 {
  while( ++p <= NF ) {
	P[$p] = p
	N[p] = $p
  }
  r++
  if( "'${no_hdr}'" != 1 ) print
  next
}
# Column definitions.
r == 1 {
  if( "'${no_hdr}'" != 1 ) print
  NR = 0
  r++
  next
}
{ split( $0, V ) }    # Fill column value array.
'"$1"'
{ out_rec = V[++i]
  while( ++i <= NF ) out_rec = out_rec FS V[i]
  print out_rec
  i=0
}'

exit 0
