#! /usr/bin/ksh if [[ "$1" = "" || "$2" = "" || "$3" = "" ]]; then echo Usage: linkcmd input_file link_file output_file echo echo This command file does many-to-many links echo with Suprtool Self-Describing files echo This generic link is done by the primary sort key. echo echo The output_file must not exist. echo echo Performance tip: echo A record group contains all records with the same key value. echo echo The input_file should be the file where the largest record echo group has the least number of records. echo echo For example, if the largest record group in FileA has 100 records echo and the largest group in FileB has 500 records, FileA should be echo the input_file and FileB should be the link_file. echo return 1 fi # # Check existence of input files # if [[ ! -s $1 ]]; then echo Sorry I can not find $1. return 1 fi if [[ ! -s $2 ]]; then echo Sorry I can not find $2. return 1 fi if [[ -s $3 ]]; then echo Sorry $3 already exists. Please delete it or choose another name. return 1 fi # # Make sure that files are self-describing # if [[ ! -s $1.sd ]]; then echo Sorry $1 is not a SelfDescribing file. return 1 fi if [[ ! -s $2.sd ]]; then echo Sorry $2 is not a SelfDescribing file. return 1 fi # {***** *****} # { This step makes sure that the files do not exist } # { already. } # {***** *****} rm outfile > /dev/null rm outfile.sd > /dev/null rm logfile > /dev/null # {***** *****} # { Make a backup copy of the link file. } # {***** *****} cp $2 $2.backup cp $2.sd $2.sd.backup # {***** *****} # { Retrieve the primary sort key name } # {***** *****} sort_key=`suprtool << !EOD | grep "<>" | cut -c11-26 fo $1 !EOD` # {***** *****} # { This step performs the initial link operation. } # { It will create and initialize all the files. } # {***** *****} echo Initial Link pass suprlink << !EOD >> logfile {***** *****} { This step will perform the initial link operation with the first } { set of records. } {***** *****} i $1 link $2 o outfile e !EOD cp outfile $3 cp outfile.sd $3.sd # {***** *****} # { This loop performs subsequent link operations } # { with the remaining records in the complete } # { secondary link file. } # { WARNING } # { The number of iterations of this loop is equal } # { to the largest number of records having the same } # { key value. } # { ex.: if 5000 records have key value 123 then } # { the loop will execute 5000 times. It also } # { means that it will read the primary link } # { file that many times. It adds up very } # { quickly. } # {***** *****} echo Starting the main loop while [[ `wc -l $2 | cut -f1 -d " "` > 0 ]]; do suprtool << !EOD >> logfile {***** *****} { This step scans the secondary link file and extracts the next set } { of records with duplicate key values. } {***** *****} i $2 sort $sort_key dupl only keys o linkfl.tmp,link e !EOD mv linkfl.tmp $2 mv linkfl.tmp.sd $2.sd # {***** *****} # { This step will perform the next link operation with the new set of } # { link records. } # {***** *****} suprlink << !EOD >> logfile i $1 link $2 o outfile,erase e !EOD # {***** *****} # { This step appends the current link results to the cumulative file. } # {***** *****} suprtool << !EOD >> logfile i outfile o $3,append e !EOD echo `wc -l $2 | cut -f1 -d " "` records left to process done echo Main loop is done. Output file contains `wc -l $3 | cut -f1 -d " "` records echo Check logfile for possible errors. # {***** *****} # { This step sorts the $3 file and displays its } # { content on the screen. } # {***** *****} suprtool << !EOD >> logfile i $3 sort $sort_key o $3.out,link e !EOD mv $3.out $3 mv $3.out.sd $3.sd # {***** *****} # { Restore the link file from the backup copy } # {***** *****} cp $2.backup $2 cp $2.sd.backup $2.sd rm $2.backup > /dev/null rm $2.sd.backup > /dev/null