I am using a cPanel account and have an Apache 2.4 access log that stores its logs like:
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"
The date is in format date “+%d/%B/%Y:%k:%M:%S”
Using a bash script I would like to extract just the lines that were logged in the last hour, for example:
Full Log file:
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
Current Time: 05/May/2018:12:01:06
Logs from: 5th of May between the time interval of 11:01 – 12:01
Filtered Output:
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
I have tried using awk and several other suggestions but I can’t get it to work, any help will be appreciated!
2
Answers
I was able to figure it out!
I had to convert the 04/May/2018:21:26:39 to a UNIX Timestamp. This is done by the following usage of date
Then make another UNIX Timestamp that's 60 minutes behind
And in an if conditional filter all log entries whose UNIX Timestamp is bigger ( -gt ) the 60 minutes behind Timestamp
With my current setup:
cPanel + Apache 2.4
Logging Format: /home/$USER/public_html_cron_logs/$DAY/$HOUR-$MINUTE-[GET|POST].log
Like /home/$USER/public_html_cron_logs/05-05-2018/14-53-GET.log
Using the time from your question to get the expected output in your question: