Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Shell脚本,用于从文件中搜索字符串并显示每小时的实例数_Linux_String_Bash_Shell - Fatal编程技术网

Linux Shell脚本,用于从文件中搜索字符串并显示每小时的实例数

Linux Shell脚本,用于从文件中搜索字符串并显示每小时的实例数,linux,string,bash,shell,Linux,String,Bash,Shell,我想编写一个shell脚本来搜索字符串“/ABCDEFG/”,并从日志中打印的时间戳中获取特定日期的每小时计数 下面是我需要从中获取计数的示例日志: 2014-06-21 00:00:22,516: |[http://123.123.123.123:15123/ABCDEFG/ [200] [REQUEST_STATUS,0;] [NO-ERROR] [134mSec] 2014-06-21 00:00:22,531: |[http://123.123.1

我想编写一个shell脚本来搜索字符串“/ABCDEFG/”,并从日志中打印的时间戳中获取特定日期的每小时计数

下面是我需要从中获取计数的示例日志:

2014-06-21 00:00:22,516:        |[http://123.123.123.123:15123/ABCDEFG/    [200]    [REQUEST_STATUS,0;] [NO-ERROR] [134mSec]
2014-06-21 00:00:22,531:        |[http://123.123.123.123:15123/ABCDEFG/    [200]    [REQUEST_STATUS,0;] [NO-ERROR] [160mSec]
2014-06-21 23:59:54,920:        |[http://123.123.123.123:15123/ABCDEFG/    [200]    [REQUEST_STATUS,0;] [NO-ERROR] [149mSec]
2014-06-21 23:59:54,923:        |[http://123.123.123.123:15123/ABCDEFG/    [200]    [REQUEST_STATUS,0;] [NO-ERROR] [164mSec]
以下是可用于获取计数的手动命令:

grep "/ABCDEFG/" abc.log |grep "2014-06-21 00" | grep "[200]"|wc -l
grep "/ABCDEFG/" abc.log |grep "2014-06-21 01" | grep "[200]"|wc -l
grep "/ABCDEFG/" abc.log |grep "2014-06-21 03" | grep "[200]"|wc -l etc

这可能适合您:

awk '$1 ~ /2014-06-21/ && $3 ~ /ABCDEFG/ && $4 == "[200]" {
         ++cnts[int(substr($2, 1, 2))]
     }
     END {
         for(i = 0; i < 24; ++i)
             printf("%02d: %4d\n", i, i in cnts ? cnts[i] : 0);
     }
' logfile
看一看。类似这样的方法会奏效:

grep '/ABCDEFG/' abc.log | grep "`date +%F`" | wc -l

您想只获取每个特定日期的
/ABCDEFG/
计数吗?是的,每天每小时的行距是否如您所示为双倍?
myscript 2014-06-21
myscript 2014-06-21 HIJKLMN
myscript 2014-06-21 ABCDEFG anotherlogfile
grep '/ABCDEFG/' abc.log | grep "`date +%F`" | wc -l