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

You cannot comment on this entry