Last Updated: February 25, 2016
·
807
· mufid

Uniq -c and Nginx Access Log

uniq -c will count the unique of lines. Example:

$ cat tes.txt
A
A
B

$ cat tes.txt | uniq -c
2 A
1 B

It would be very hand when we are dealing with "different lines but we want to see the number of grouped same partial information". For example, we want to count request per minute from nginx access.log. We know the default nginx access log is like this (your configuration may vary):

127.0.0.1 - - [13/Feb/2015:21:16:29 -0500] "GET /install.php HTTP/1.1" 502 583 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"

Then simply:

$ tail -9000 access.log | grep '" 200 ' | cut -d: -f2,3 | uniq -c
 19 18:02
 29 18:03
 43 18:06
 32 18:07
 15 18:08
 29 18:09

to get request per minute of OKs in the last 9000 hits.

(Further Reading)