O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Real-time IP address lookup for requests

In the following example, we make use of tail -f to keep reading the new entries of log files and pass them to the AWK command for processing. Here also, we use the geoiplookup command to fetch the IP address information and print it on screen, as follows:

$ tail -f apache_logs.txt |awk '{print "IPADDRESS : " $1};{ system("geoiplookup " $1)};{print "REQUEST LINE :"$6,$7,"\n""STATUS CODE : "$9}'

We can also print other information such as the user agent in a desired format in output by setting the field separator to double quotes ( " ) and splitting the first and third field into an array to fetch the information, as follows:

$tail -f apache_logs.txt | awk -F"\"" 'split($1,a," "){print "IPADDRESS : ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required