Home > Software engineering >  How to write a Bash script / command that shows the range of 500-511 HTTP server errors in a file
How to write a Bash script / command that shows the range of 500-511 HTTP server errors in a file

Time:02-21

I am trying to write a bash script that will list and count the number of HTTP: 500 - 511 web error inside this file "ccc2022-02-19.txt" Inside every file there are several 500 errors ranging from HTTP 500, 501, 502, 503 up to 511.

Within the directory where this files are , there are 4 different type of files listed there daily but I am only interested on the files that starts with "ccc" because they are listed daily for example "ccc2022-02-19.txt", "ccc2022-02-20.txt" etc

Below is an example of the file content "ccc2022-02-19.txt"

10.32.10.181    ignore  19 Feb 2022 00:26:04 GMT        10.32.10.44     GET / HTTP/1.1  500     73      N       0       h
10.32.26.124    ignore  19 Feb 2022 00:26:06 GMT        10.32.10.44     GET / HTTP/1.1  501     73      N       0       h
10.32.42.249    ignore  19 Feb 2022 00:26:27 GMT        10.32.10.44     GET / HTTP/1.1  500     73      N       1       h
10.32.10.181    ignore  19 Feb 2022 00:26:34 GMT        10.32.10.44     GET / HTTP/1.1  302     73      N       0       h
10.32.26.124    ignore  19 Feb 2022 00:26:36 GMT        10.32.10.44     GET / HTTP/1.1  503     73      N       1       h
10.32.26.124    ignore  19 Feb 2022 00:26:36 GMT        10.32.10.44     GET / HTTP/1.1  502     73      N       1       h
10.32.26.124    ignore  19 Feb 2022 00:26:36 GMT        10.32.10.44     GET / HTTP/1.1  502     73      N       1       h
10.32.26.124    ignore  19 Feb 2022 00:26:36 GMT        10.32.10.44     GET / HTTP/1.1  504     73      N       1       h
10.32.26.124    ignore  19 Feb 2022 00:26:36 GMT        10.32.10.44     GET / HTTP/1.1  511     73      N       1       h
10.32.26.124    ignore  19 Feb 2022 00:26:36 GMT        10.32.10.44     GET / HTTP/1.1  508     73  

I have tried using this command

awk '{for(i=1;i<=NF;i  ){if($i>=500 && $i<=511){print $i}}}' ccc2022-02-19.txt

which listed the numbers 500 -511 but I'm afraid that it is not giving only the HTTP response but grepped other number too like 50023, 503893 found inside the file.

To be specific, I just want to see only the HTTP errors. Please note that the file content above is just an example......

CodePudding user response:

Here is a simple awk script:

awk '$12 ~ /5[[:digit:]]{2}/ && $12 < 512 {print $12}' input.txt

Explanation

$12 ~ /5[[:digit:]]{2}/ Field #12 match 5[0-9][0-9]

$12 < 512 Field #12 less than 12

$12 ~ /5[[:digit:]]{2}/ && $12 < 512 (Field #12 match 5[0-9][0-9]) AND (Field #12 less than 12)

{print $12} Print field #12 only if 2 conditions above are met

CodePudding user response:

This should achieve what you want. Please guys always try to read the description before concluding that he asked a stupid question. It is actually clear!!

awk '{print $3 " " $4 " " $5 " " $6 " " $11 " " $12}' ccc2022-02-21.txt | grep 500 | wc -l

This experiment was done in reference to the file output he provided above and i tested this and it worked! This was a brilliant question in my opinion

CodePudding user response:

I think this script might help

#!/bin/bash

ccc=500
while [ $ccc -le 511 ]
do 
  echo $ccc
  ccc=$(( $ccc  1 ))
  sleep 0.5
done

CodePudding user response:

You can try out this:

#!/bin/bash
CURRENTDATE=`date  "%Y-%m-%d"`
echo Today date is=${CURRENTDATE}
echo Looking for today file www${CURRENTDATE}.txt
echo "#####"

echo Start listing 500 response codes for file:ccc${CURRENTDATE}.txt
#awk '{print $3 " " $4 " " $5 " " $6 " " $11}' ccc${CURRENTDATE}.txt | grep 500
echo "I am not listing to reduce amount of lines per Ms-teams limit"
echo Completed listing 500 response codes for file:ccc${CURRENTDATE}.txt
echo "#####"

CodePudding user response:

Assuming all lines look like the sample (ie, the http error code is always in the 12th white-space delimited field):

$ awk '$12>= 500 && $12<=511 {print $12}' ccc2022-02-19.txt
500
501
500
503
502
502
504
511
508

If this doesn't work for all possible input lines then the question should be updated with a more representative set of sample data.

  • Related