I am trying to parse a log and get the lines between timestamp.Tried sed approach like below but facing issue with regex
Log pattern:
IP - - [20/Apr/2018:14:25:37 +0000] "GET / HTTP/1.1" 301 3936 "-" "
IP - - [20/Apr/2018:14:44:08 +0000]
----------------------------------
IP- - [20/Apr/2018:20:43:46 +0000]
I need to get the lines between 14:25
and 20:43
for 20th april as the log contains other dates also.
Tried this:
sed -n '/\[14:25/,/\[20:43/p' *-https_access.log.1
but not working.
To print lines between
match1
andmatch2
with sed or awk you can do:in your example
match1
is20/Apr/2018:14:25
andmatch2
is20/Apr/2018:20:43
. So any of these commands should work for you:or use
|
as a sed's delimiter to prevent escaping slash:sed is not appropriate because it's hard to compare element (like day and hour).
with awk (self commented):
more generaly, we can use variable to give date and hour as paramter to the awk (not the purpose here)
Since you mentioned you want logs for 20th April, I'd suggest something like :
This is very less likely to conflict with false matches in case "20:43" occurs elsewhere.
The best solution is to use
awk
for this. What you need to do is convert your time-stamps to a unix-time and then do the comparisons. Inawk
you can do this usingmktime()
In order to convert your time-format of the form
20/Apr/2018:14:25:37 +0000
into2018 04 20 14 25 37 +0000
This method is robust as it will do true time comparisons which are independent of leap years, day/month/year-cross-overs, ... In contrast to other solutions provided, this method also does not require the existence of the date
tstart
andtend
in thefile