需要在 Apache 日志中找到 1-3 秒的响应时间

需要在 Apache 日志中找到 1-3 秒的响应时间

我需要从 Apache 日志文件中查找 API 响应时间。这就像一个响应时间,需要 1 到 2 秒或 2 到 3 秒。$6是响应时间,值以微秒为单位。

我正在尝试使用以下命令,但输出始终相同:

grep 17/Sep/2016:10 /access.log| awk '{print ($6 > 1000000 && 2000000 > $6)}' | wc -l

答案1

如果添加一些行access.log作为示例,这个问题会更清楚。无论如何,无论 的值如何,awk 命令都会打印一行$6,因此当您计算行数时,wc -l您会得到仅由 grep 确定的结果。

如果你想计算$6两个不同值之间的行数,你可以写

grep 17/Sep/2016:10 /access.log | awk '$6 > 1000000 && 2000000 > $6' | wc -l

然而,这个管道的效率有点低。将其组合在单个 awk 命令中几乎总是更好,如下所示:

awk '/17\/Sep\/2016:10/ && $6 > 1000000 && 2000000 > $6 {c++} END{print c}' access.log

要包含边界,可以执行以下操作:

grep 18/Sep/2016:11 /access.log | awk ' $6>=1000000 && $6<=2000000' | wc -l

或同等地

awk '/18\/Sep\/2016:11/ && $6>=1000000 && $6<=2000000 {c++} END{print c}' access.log

答案2

使用(以前称为 Perl_6)

条件字符串仅返回第一列中匹配的行(不需要grep)。该.words例程在空白处进行分割。使用ne而不是eq省略匹配行

~$ raku -ne '.put if .words[0] eq "Mar";'  file
Mar 2nd 3rd 4th 5th 1000002 7th

链接数字不等式以返回第六列中具有所需值的行:

~$ raku -ne '.put if 1000000 <= .words[5] < 3000000;'  file
Feb 2nd 3rd 4th 5th 1000001 7th
Mar 2nd 3rd 4th 5th 1000002 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th

将条件语句放在一起:

~$ raku -ne '.put if .words[0] ne "Mar"  &&  1000000 <= .words[5] < 3000000;'   file
Feb 2nd 3rd 4th 5th 1000001 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th

使用行计数器作为最终输出:

~$ raku -ne 'BEGIN my $line_cnt; ++$line_cnt if .words[0] ne "Mar"  &&  1000000 <= .words[5] < 3000000; END put $line_cnt;'   file
4

输入示例:

Jan 2nd 3rd 4th 5th 999999 7th
Feb 2nd 3rd 4th 5th 1000001 7th
Mar 2nd 3rd 4th 5th 1000002 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th
Jul 2nd 3rd 4th 5th 3000001 7th

https://docs.raku.org/language/operators#Tight_AND_precedence
https://docs.raku.org/language/phasers
https://raku.org

答案3

使用珀尔

条件字符串仅返回第一列中匹配的行(不需要grep)。使用ne而不是eq省略匹配行

~$ perl -lane 'print if $F[0] eq "Mar";'  file
Mar 2nd 3rd 4th 5th 1000002 7th

返回第六列中具有所需值的行的数值不等式:

~$ perl -lane 'print if $F[5] >= 1000000 && $F[5] < 3000000;'  file
Feb 2nd 3rd 4th 5th 1000001 7th
Mar 2nd 3rd 4th 5th 1000002 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th

将条件语句放在一起:

~$ perl -lane 'if (($F[0] ne "Mar") && ($F[5] >= 1000000) && ($F[5] < 3000000)) {print $_};'  file
Feb 2nd 3rd 4th 5th 1000001 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th

使用行计数器作为最终输出:

~$ perl -lane 'BEGIN {$line_cnt}; if (($F[0] ne "Mar") && ($F[5] >= 1000000) && ($F[5] < 3000000)) {++$line_cnt}; END {print $line_cnt};'  file
4

输入示例:

Jan 2nd 3rd 4th 5th 999999 7th
Feb 2nd 3rd 4th 5th 1000001 7th
Mar 2nd 3rd 4th 5th 1000002 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th
Jul 2nd 3rd 4th 5th 3000001 7th

https://en.wikibooks.org/wiki/Perl_Programming/Conditionals
https://stackoverflow.com/questions/16678272/perl-script-to-count-words-lines
https://unix.stackexchange.com/a/727962/227738

相关内容