Linux awk question
Author
Discussion

TheExcession

Original Poster:

11,669 posts

274 months

Thursday 7th October 2004
quotequote all
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

Pigeon

18,535 posts

270 months

Friday 8th October 2004
quotequote all
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

zumbruk

7,848 posts

284 months

Friday 8th October 2004
quotequote all
Eek. Do it in Perl...

TheExcession

Original Poster:

11,669 posts

274 months

Friday 8th October 2004
quotequote all
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

shadrach

34 posts

267 months

Friday 8th October 2004
quotequote all
I told The Ex to do it in Perl, but he's got well into awk.

Shadrach

TheExcession

Original Poster:

11,669 posts

274 months

Friday 8th October 2004
quotequote all
It's all well and good saying do it in Perl - but I don't really know any Perl so unless someone can type me some code or point me to something very similar...

[tongueoutsmilie]

best
Ex

GreenV8S

30,999 posts

308 months

Friday 8th October 2004
quotequote all
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.

zumbruk

7,848 posts

284 months

Friday 8th October 2004
quotequote all
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

18,535 posts

270 months

Friday 8th October 2004
quotequote all
zumbruk said:
Good job you don't work for me ...

What is your position regarding the use of Duff's device?

GreenV8S

30,999 posts

308 months

Friday 8th October 2004
quotequote all
zumbruk said:


Good job you don't work for me ...


Hey, I never said it was elegant! But without looking into what he is trying to achieve it is easier just to make a tactical bodge to get the desired effect than to fix the design to do it cleanly.

zumbruk

7,848 posts

284 months

Saturday 9th October 2004
quotequote all
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.