2017-04-25 167 views
-3

我想編寫一個腳本,將執行命令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。 –

回答

0

當你在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 
0

腳本是一行。

#!/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