I occasionally use this line in my terminal to see which user-agent is using my server more.
cat /var/log/apache2/access.log | awk -F\" '{print $6}' | sort | uniq -c | sort -n
Scans the access.log and show in ascending order the user-agents that it has found several times.
The result is something like this:
10283 Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1
23247 Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36
40063 MauiBot ([email protected])
143724 Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
192741 Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)
I have no idea how it works, so long ago I found it somewhere or someone gave me the date, I do not remember.
Anyway, is possible do the same thing but ordering for IP?
I think IP is the on the first column in the log, try
Judging from the fact that you're using
awk -F \" { print $6}
there, this appears like combined log format, which should have the IP address in field$1
. From there, we can just do uniq in awk, then combine that with sorting indices in associative arrays.With a mock
access.log
file like this:the
awk
code should be as so:For numeric sorting by the number of entries for each ip address, we can just stick
sort
at the end:Of course, you should use
/var/log/apache2/access.log
as your actual input file.