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

Printing the top 10 request IP addresses with their GeoIP information

In this example, we first find the top 10 IP addresses which made requests to our website based on their request count, as shown in previous example. Then, we further use the system command to run the GeoIP package to print the geographical information of requesting IP address. The GeoIP package is freely available in the debian repository and the EPEL repository for rpm-based operating systems:

$ awk '{ print $1}' apache_logs.txt | sort| uniq -c | sort -rn | head | awk '{ print $2 };{print "================="};system("geoiplookup " $2);{print "================="}'

     Or:

$ awk '{freq[$1]++} END {for (x in freq) {print freq[x], x}}' apache_logs.txt | sort -rn | head | ...

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