How to get list of user-agents from nginx log?


This is actually easy to do and works with our precious cli tools:

awk -F'"' '/GET/ {print $6}' /var/log/nginx/access.log | cut -d' ' -f1 | sort | uniq -c | sort -rn

And here is what happens in detail:

awk - selecting full User-Agent string of GET requests
cut - fetching only the first word of that string
sort - sorting
uniq - counting
sort - sorting by count, reversed

Here is an alternative version that is faster on bigger log files:

sed -n 's!.* "GET.* "\([[:alnum:].]\+/*[[:digit:].]*\)[^"]*"$!\1!p' /var/log/nginx/access.log | sort | uniq -c | sort -rfg
Last update:
31-07-2013 17:53
Author:
Daniel Urstöger
Revision:
1.1
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.