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

Using AWK for processing different log fields 

AWK is used here to split the lines of the log file into fields or columns, using a default separator. Since each line in an Apache log file follows a standard format, we can process it easily with AWK. We will be using a default separator (spaces or tabs) to parse the log file sample line used previously, as follows:

$ vi sample.txt83.149.9.216 - - [17/Feb/2018:10:05:03 +0000] "GET /presentations/images/search.png HTTP/1.1" 200 203023 "http://semicomplete.com/presentations/2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"
  • Print IP address (%h):
$ awk '{print $1}' sample.txt
  • Print RFC 1413 identity (%l):
$ awk ...

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