Monday, August 18, 2008

Numeric int function in awk


wrt.log contains some consolidated log information of an application for a whole day (00:00:00 IST to 23:59:59 IST in a particular day). The format of the log lines is:

Timestamp IP Status

we need to find out the IPs corresponding to status "ACT" and of the present hour timestamp.

i.e.

input file:

$
cat wrt.log
05:18:37 IST 2008 172.21.45.2 ACT
09:18:27 IST 2008 172.21.45.12 ACT
06:18:37 IST 2008 172.21.45.22 DES
08:18:37 IST 2008 172.21.45.3 ACT
00:18:37 IST 2008 172.21.45.23 DES
09:18:39 IST 2008 172.21.45.9 DES

And present date:

$ date
Mon Aug 18 09:23:21 IST 2008

$ date +%H
09

so output required is:

172.21.45.12

The awk solution:

$ awk -v hour=$(date +%H) '
int($1) == hour && /ACT/ {
print $4
}
' wrt.log

Things to digest: Use of numeric int function within awk.

No comments:

© Jadu Saikia www.UNIXCL.com