如何编写 Bash 脚本/命令来显示文件中 500-511 HTTP 服务器错误的范围

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

我正在尝试编写一个 bash 脚本,该脚本将列出并计算此文件“ccc2022-02-19.txt”中的 HTTP: 500 - 511 网络错误的数量 每个文件中都有几个 500 错误,范围从 HTTP 500、501、502、503 到 511。

在这个文件所在的目录中,每天列出 4 种不同类型的文件,但我只对以“ccc”开头的文件感兴趣,因为它们每天都列出,例如“ccc2022-02-19.txt", "ccc2022-02-20.txt" 等

以下是文件内容“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  

我试过使用这个命令

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

其中列出了数字 500 -511,但我担心它不仅提供了 HTTP 响应,还像 50023,在文件中找到 503893。

具体来说,我只想查看 HTTP 错误。请注意,以上文件内容仅为示例......

我认为这个脚本可能会有所帮助

#!/bin/bash

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

你可以试试这个:

#!/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  " "  " "  " "  " " }' 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 "#####"

假设 所有 行看起来像示例(即 http 错误代码总是在第 12 个 white-space 分隔字段中):

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

如果这不适用于所有可能的输入行,则应使用更具代表性的样本数据集更新问题。

这是一个简单的 awk 脚本:

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

说明

~ /5[[:digit:]]{2}/ 字段 #12 匹配 5[0-9][0-9]

< 512 字段 #12 小于 12

~ /5[[:digit:]]{2}/ && < 512(字段 #12 匹配 5[0-9][0-9])AND(字段 #12 小于 12)

{print } 仅当满足上述 2 个条件时才打印字段 #12

这应该能达到你想要的效果。请大家在断定他问了一个愚蠢的问题之前总是尝试阅读描述。其实很清楚!!

awk '{print  " "  " "  " "  " "  " " }' ccc2022-02-21.txt | grep 500 | wc -l

这个实验是参考他上面提供的文件输出完成的,我测试了这个并且成功了!我认为这是一个绝妙的问题