Count all http response status codes in a web application log files on linux

Count all http response status codes in a web application log files on linux

Advertisement

Here is one way to count all the HTTP response status codes in a web application log file on Linux:


  1. Open the log file using the cat command:



cat log_file



  1. Use the grep command to search for lines that contain an HTTP status code:



cat log_file | grep -Eo 'HTTP/[0-9\.]+ [0-9]+'


This will output all the lines in the log file that contain an HTTP status code.

  1. Use the awk command to extract the status code from each line:



cat log_file | grep -Eo 'HTTP/[0-9\.]+ [0-9]+' | awk '{print $2}'


This will output a list of all the status codes in the log file.

  1. Use the sort and uniq commands to count the number of occurrences of each status code:



cat log_file | grep -Eo 'HTTP/[0-9\.]+ [0-9]+' | awk '{print $2}' | sort | uniq -c


1- Sample Log file:

Given the log file below, we want to know all http response status code and sort them from the highest amount of response code to the lowest.

2- Use this command below to Count all http response status codes in a web application log files on linux:

awk '{print $9}' sample_log.log | sort | uniq -c | sort -rn

3- Output

Count Each unique Response Status Code from Web App log file

Learn more about http response status code here

Filed under

Related articles

Level up your Tech Career with DjamgaTech PRO

Download the DjamgaTech PRO App on the Apple App Store to access hundreds of interactive prep questions, flashcards, and quizzes cleanly without ads.

Djamgatech PRO App
Download DjamgaTech PRO on the App Store 🚀

← All articles