Skip to content
Advertisement

unix command to search count of 5XX status HTTP requests in the log

i need to write a unix command to search count of 5XX status HTTP requests in the log. Log format is like below example :

192.168.0.101 - - [12/Dec/2019:23:15:56 +0900] "GET /index.html HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0" 

any suggestions ?

Advertisement

Answer

A minimal solution would be

$ grep "5d{2}" somefile.log | wc -l

grep will filter the output to only the lines including the string 5XX, where X is any digit character. This output is then piped to wc -l, which counts the number of lines produced.

However, it is possible that the string 5XX will occur elsewhere in the log file. To account for this, you may wish to consider using a more detailed pattern in grep, such as

$ grep "HTTP/1.1" 5d{2}" somefile.log | wc -l

which ensures that you only match HTTP error codes and not, e.g. a future Gecko version.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement