#!/bin/bash
#<pre><b>
# Script     : splitbiglst
# Version    : 1.0
# Author     : Roland Rashleigh-Berry
# Date       : 16-Aug-2005
# Purpose    : Production script to split a big .lst file into separate files
# SubScripts : none
# Notes      : This splits concatenated multiple SAS table/listing outputs into
#              separate outputs assuming the first line of each output ends with
#              "Page 1 of nn*". The output files are named file001.tmp,
#              file002.tmp etc. Files matching this pattern will be deleted 
#              beforehand.
# Usage      : splitbiglst t_output.lis
#              
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
#  1   Name of big output file
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# 
#===============================================================================
# This is public domain software. No guarantee as to suitability or accuracy is
# given or implied. User uses this code entirely at their own risk.
#===============================================================================

# One input file only
if [ $# -ne 1 ] ; then
  echo "Usage: splitbiglst bigoutput.lst" 1>&2
  exit 1
fi

# remove files matching the output pattern
rm -f file[0-9][0-9][0-9].tmp


# gawk program starts here
gawk '

# initialise doc to zero
BEGIN {doc=0}

{

# if a line ends with "Page 1 of nn*"
if ($0 ~ / Page 1 of [0-9][0-9]*/ )
  {
  if (doc>0)
    close(filename)
  
  doc++
  filename="file" substr(1000+doc,2) ".tmp"

  if (doc>1)
    print substr($0,2) >> filename
  else
    print $0 >> filename
  }

else
  print $0 >> filename

}' "$1"
