#****************************************************************************
#  ##   ##         #####   #####  ##     **        NoSQL RDBMS - weed       *
#  ###  ##        ####### ####### ##     **        $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Written by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
#
#  Removes invalid records from a table.
#
#  Discards any records whose number of TAB-separated fields does not match
#  the number of fields in the table header. The failing rows are optionally
#  written to the file specified on the command line, or to /dev/null if
#  no output file is specified.
#
#  This NoSQL operator reads a table from STDIN and writes the purified
#  table to STDOUT.
#
########################################################################

########################################################################
# BEGIN block
########################################################################

BEGIN \
{
  NULL = ""; FS = OFS = "\t";

  # Get command line arguments.
  split( __nosql_args, args, " " )

  while ( args[++i] != NULL )
  {
	if ( args[i] == "-o" || args[i] == "--output" ) continue
	else out_file = args[i]
  }

  if ( out_file == NULL ) out_file = "/dev/null"
}

########################################################################
# Main loop
########################################################################

# Get the number of fields in the table header.
NR == 1 { num_fields = NF }
NR > 2 && NF != num_fields { print > out_file ; next }

{ print }

