Passing date into awk...

Hi All....

I need to pass date into awk and parse logfile based on that.... i used both awk and /usr/xpg4/bin/awk... both are throwing up error.....

So here is the stuff...

when i use /usr/xpg4/bin/awk :

DATE=`date '+%Y %b %d'`
START=00
END=23
/usr/xpg4/bin/awk -v DATE={"$DATE"} -v START=$START -v END=$END '/DATE START/,/DATE END/' logfile.log  > processfile.txt

Error :

/usr/xpg4/bin/awk: inadmissible use of reserved keyword  Context is:
>>>     {2010 Jan 02}1023       <<<

when i use awk:

DATE=`date '+%Y %b %d'`
START=00
END=23
awk -v DATE={"$DATE"} -v START=$START -v END=$END '/DATE START/,/DATE END/' logfile.log  > processfile.txt

Error:

awk: syntax error near line 1
awk: bailing out near line 1

Can anyone think of y this error is happening??? How to finally pass date into awk???

Try something like:

awk -v dd="$DATE" -v s=$START -v t=$END '$0~dd FS s,$0~dd FS t' logfile.log

You cannot use END as a variable name, it is a reserved word....

1 Like

Scrutinizer...

The Error was spot on!!! Cannot use END and variable name!!!

/usr/xpg4/bin/awk -v s="$START" -v e="$END" '/s/,/e/' logfile.log  > processfile.txt

Works perfectly fine!!!

Doubt your posted code will work as you intended. I feel it will list everything from first record that contains the letter 's' to next record that contains letter 'e'

You should probably do:

/usr/xpg4/bin/awk -v s="$START" -v e="$END" '$0~s,$0~e' logfile.log  > processfile.txt