猿问

Apache-最近一小时的日志

我正在使用cPanel帐户,并具有一个Apache 2.4访问日志,该日志存储其日志,例如:


66.249.93.30 - - [04/May/2018:21:26:39 +0200] "GET / HTTP/1.1" 302 207 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko; Google Page Speed Insights) Chrome/41.0.2272.118 Safari/537.36"

66.249.93.30 - - [05/May/2018:10:26:39 +0200] "GET / HTTP/1.1" 302 207 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko; Google Page Speed Insights) Chrome/41.0.2272.118 Safari/537.36"

该日期的格式为日期“ +%d /%B /%Y:%k:%M:%S”


使用bash脚本,我只想提取最近一小时记录的行,例如:


完整的日志文件:


66.249.93.30 - - [04/May/2018:21:26:39 +0200] First Line

66.249.93.30 - - [05/May/2018:11:00:21 +0200] Second Line

66.249.93.30 - - [05/May/2018:11:15:39 +0200] Third Line

66.249.93.30 - - [05/May/2018:12:00:11 +0200] Fourth Line

当前时间: 05 / May / 2018:12 : 01:06


日志从: 5月5日11:01-12:01之间的时间间隔开始


过滤后的输出:


66.249.93.30 - - [05/May/2018:11:15:39 +0200] Third Line

66.249.93.30 - - [05/May/2018:12:00:11 +0200] Fourth Line

我已经尝试过使用awk和其他一些建议,但是我无法使其正常工作,我们将不胜感激!


繁花不似锦
浏览 259回答 2
2回答

RISEBY

$ dateSat, May 05, 2018 10:49:13 AM$ cat tst.awk{    split($4,t,/[[ :\/]/)    mthNr = sprintf("%02d",(index("JanFebMarAprMayJunJulAugSepOctNovDec",t[3])+2)/3)    curTime = t[4] mthNr t[2] t[5] t[6] t[7]}curTime >= minTime$ awk -v minTime=$(date -d '60 min ago' '+%Y%m%d%H%M%S') -f tst.awk file66.249.93.30 - - [05/May/2018:11:00:21 +0200] Second Line66.249.93.30 - - [05/May/2018:11:15:39 +0200] Third Line66.249.93.30 - - [05/May/2018:12:00:11 +0200] Fourth Line使用问题的时间来获得问题的预期输出:$ awk -v minTime=$(date -d '2018/05/05 11:01:06' '+%Y%m%d%H%M%S') -f tst.awk file66.249.93.30 - - [05/May/2018:11:15:39 +0200] Third Line66.249.93.30 - - [05/May/2018:12:00:11 +0200] Fourth Line
随时随地看视频慕课网APP
我要回答