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

Counting and printing unique visits to a website

  • Print the total number or unique visitors: In this example, we again fetch the IP address information from field 1 and then pipe it to the shell commands to fetch the total number of unique visitors to our website as follows:
 $ awk '{ print $1 }' apache_logs.txt | sort | uniq -c| wc -l

     Or:

$ cat apache_logs.txt | awk '{ print $1 }' apache_logs.txt | sort | uniq -c| wc -l

     Or:

$ awk '{ freq[$1]++} END {for (x in freq) {print freq[x], x} }' apache_logs.txt | wc -l
  • Print the total number of unique visitors today:
$ cat apache_logs.txt | grep `date '+%e/%b/%G'`| awk '{ print $1 }'| sort | uniq -c| wc -l

Or:

$ cat apache_logs.txt | grep `date '+%e/%b/%G'`| awk '{ freq[$1]++} END {for ...

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