shell脚本与时间执行grep和提供文件输出

问题描述:

我想编写一个脚本,将执行命令shell脚本与时间执行grep和提供文件输出

cat file.txt | grep -c 'HH:MM' [这里HH:MM会从00:00到23:59] 以及输出的内容将以 00:00 - 2134 00:01 - 3127 的格式保存在文件中。 。 。 。 23:59 - 1298

在此先感谢。

+0

你的问题是什么? – jehutyy

+0

“grep -Eo”[0-2] [0-9]:[0-5] [0-9]“|排序| uniq -c'? –

+0

我的服务器中有一个文件,其中请求来自移动设备并且数量巨大(比如每分钟超过100个请求)。我想获得请求的数量,以便我可以计算出请求的时间很长。所以当我执行'cat file.txt | grep -c '10:30'它会在10点30分钟给出信息数量。同样会得到00:00,00:01,00:02 ..... ..... 23:59。 –

当你在grep命令添加文件名,则可以避免额外的命令cat
当access.log已经在日期排序时,您可以跳过排序。

grep -Eo "2017-04-25T[0-2][0-9]:[0-5][0-9]" /local/logs/access.log | uniq -c > /tmp/list.txt 

随着你必须知道,有人可以“攻击”脚本(使统计失败)通过tryint进入电影的网页http://yoursite/2017-04-25T11:11/fooledyou.html
可以使用sed更多的控制你如何选择子

的access.log
# test remove from start of line until the first [ and also remove the [, 
# remember the matched date and remove the rest, replace by remembered string. 
echo "127.0.0.1 - - [2017-04-25T11:11 +0000] GET /2017-04-25T22:22/fooledyou.html HTTP/1.1 200 140 - Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.5 Safari/535.19" | 
    sed 's/[^\[]*\[\(2017-04-25T[0-2][0-9]:[0-5][0-9]\).*/\1/' 

# your command 
sed 's/[^\[]*\[\(2017-04-25T[0-2][0-9]:[0-5][0-9]\).*/\1/' /local/logs/access.log | 
    uniq -c > /tmp/list.txt 

脚本是一行。

#!/bin/bash 
cat /local/logs/access.log | grep -Eo "2017-04-25T[0-2][0-9]:[0-5][0-9]" | sort | uniq -c > /tmp/list.txt 

执行后,我得到一个文件list.txt,当我打开它,得到如下所述的列表。

Output of the script