Linux awk question
Discussion
Sorry - I can't get spaces to work in the post to make this a bit clearer...
I'm still trying to get to grips with bash and awk and stuff.
But I'm struggling with one problem. My awk block in my shell sctipt looks like this
awk ' BEGIN {
# we want to keep a count of the number of records
# found in each file - print this to the summary file later
RecordCount = 0;
}# fi BEGIN
# main awk block
{
# record field format:
#$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12
#MPDS 2004-09-22 14:22:12.387361 1 989680 989681 00012 REGSITERED 0 002 1001 1002
# Test the record
if ( $1==CallType &&
(UEType=="Any" || UEType==$4) &&
(RtnID=="Any" || RtnID==$5) &&
(FwdID=="Any" || FwdID==$6) &&
(LES=="Any" || LES==$7) &&
(ConnectionState=="Any" || ConnectionState==$8) )
{
RecordCount++;
# print out the Filename, Date, Time and State to the results file
# the result file will be used by mergecap later
printf("%s %s %s %s", FILENAME, $2, $3, $8 ) >> ResultFile
} # fi test the record
} # end main awk block
END {
# print out a summary of records found in each file
printf("%s %s", RecordCount, FILENAME) >> SummaryFile
}' CallType=$CALLTYPE
UEType=$UETYPE
RtnID=$RTNMESID
FwdID=$FWDMESID
LES=$LES
ConnectionState=$CONNECTIONSTATE
ResultFile=$RESULTSFILE.results
SummaryFile=$RESULTSFILE.summary
${CANDIDATE_FILE_LIST[$a]}
Now what I'd like to do is specify more than one paramter for searching some of the fields.
For example field $8 currently showing as REGISTERED has some other values too.
Is there a way that I can pass in an array of values to compare to $8 - say 'REGISTERED' and 'CONNECTED' and then use these to get the records that match?
As you can see I am already handlng a case of "Any" - but I'd like to get some tidy code to handle dynamic multiple options for certain fields.
many thanks
Ex
>>> Edited by TheExcession on Thursday 7th October 18:28
I'm still trying to get to grips with bash and awk and stuff.
But I'm struggling with one problem. My awk block in my shell sctipt looks like this
awk ' BEGIN {
# we want to keep a count of the number of records
# found in each file - print this to the summary file later
RecordCount = 0;
}# fi BEGIN
# main awk block
{
# record field format:
#$1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12
#MPDS 2004-09-22 14:22:12.387361 1 989680 989681 00012 REGSITERED 0 002 1001 1002
# Test the record
if ( $1==CallType &&
(UEType=="Any" || UEType==$4) &&
(RtnID=="Any" || RtnID==$5) &&
(FwdID=="Any" || FwdID==$6) &&
(LES=="Any" || LES==$7) &&
(ConnectionState=="Any" || ConnectionState==$8) )
{
RecordCount++;
# print out the Filename, Date, Time and State to the results file
# the result file will be used by mergecap later
printf("%s %s %s %s", FILENAME, $2, $3, $8 ) >> ResultFile
} # fi test the record
} # end main awk block
END {
# print out a summary of records found in each file
printf("%s %s", RecordCount, FILENAME) >> SummaryFile
}' CallType=$CALLTYPE
UEType=$UETYPE
RtnID=$RTNMESID
FwdID=$FWDMESID
LES=$LES
ConnectionState=$CONNECTIONSTATE
ResultFile=$RESULTSFILE.results
SummaryFile=$RESULTSFILE.summary
${CANDIDATE_FILE_LIST[$a]}
Now what I'd like to do is specify more than one paramter for searching some of the fields.
For example field $8 currently showing as REGISTERED has some other values too.
Is there a way that I can pass in an array of values to compare to $8 - say 'REGISTERED' and 'CONNECTED' and then use these to get the records that match?
As you can see I am already handlng a case of "Any" - but I'd like to get some tidy code to handle dynamic multiple options for certain fields.
many thanks
Ex
>>> Edited by TheExcession on Thursday 7th October 18:28
I'm no awk expert, but this page would seem to be of use to you:
www.gnu.org/software/gawk/manual/html_node/Arrays.html
www.gnu.org/software/gawk/manual/html_node/Arrays.html
Pigeon said:
I'm no awk expert, but this page would seem to be of use to you:
www.gnu.org/software/gawk/manual/html_node/Arrays.html
Thanks Pigeon - good link - plenty to chew on there.
best
Ex
GreenV8S said:
You could brute force it by passing in the array of values in the form of a string with the values separated by some recognisable character, parse the string back into an array during initialisation and provide a method to compare $8 against the array.
Good job you don't work for me ...

Pigeon said:
zumbruk said:
Good job you don't work for me ...
What is your position regarding the use of Duff's device?![]()
*grin* It's hideous. CPU cycles are cheap and development time is expensive (outsourcing notwithstanding). The only way I'd tolerate something like that is if it had 20 lines of explanatory comments around it, so that the poor soul who has to maintain it once the author has moved on will understand what's going on.
'C' - all the power of assembler with all the clarity and maintainability of ... assembler.
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff



