awk -f frust.awk fileWITHranges.txt fileWITHdata.txt
here's frust.awk:
Code:
# BLOCK 1
# FNR==NR is true when dealing with the FIRST file - file with ranges
FNR==NR {
# save the customer name from the SECOND field in the array 'cust' indexed
# by the record/line number
cust[FNR]=$2
# split the LAST [$NF] field [range specification] by '-' and save the results
# in an array 't'
split($NF, t, "-")
# save the LOW limit of the range 't[1]' in an array 'custLow' indexed by
# currect record/line number
custLow[FNR]=t[1]
# do the same as above for the HIGH limit in the range
custHigh[FNR]=t[2]
# save the currect record/line number in a variable
maxType=FNR
# jump to the NEXT record/line
next
}
# BLOCK 2
# Here we're dealing with the OTHER file specified on the command line
# fileWITHdata.txt - line by line.
{
# locate the pattern in a line - from the beginning of the line [^] followed
# by one or more digits - [0-9][0-9]*
match($0, /^[0-9][0-9]*/)
# extract the sub-string from the line [$0] starting at position '1' for the
# 'RLENGTH' number of characters - 'RLENGTH' gets set by the previous
# 'match' function when the pattern was searched/matched - the return
# value is the NUMBER.
num=substr($0, 1, RLENGTH)
# iterate from '1' to the maxType - 'maxType' was the number of
# lines/records processed in 'BLOCK 1' - number of 'range' specifications.
for(i=1; i <= maxType; i++) {
# determine if the current number 'num' falls within one of the range arrays
# populated in 'BLOCK 1'
if ( num >= custLow[i] && num <= custHigh[i] )
# If it does, increment the number the value in the array 'final' which is
# indexed by the cutomerName - the csutomerName was saved in 'BLOCK 1'
# and was indexed by the record/line number which is our iterator in this
# surrounding 'for' loop.
final[cust[i]]++
}
}
# END of processing block - this gets executed when ALL the files have been
# processed
END {
# iterate through ALL the indecies 'i' in array 'final' - 'i' contains the name of
# a customer. The value of a given array cell is the NUMBER of times a
# particular customer 'fell' within the specified range(s). Print out the
# results.
for (i in final)
printf("%s -> [%d]\n", i, final[i])
}