I want to extract all logs between two timestamps. Some lines may not have the timestamp, but I want those lines also. In short, I want every line that falls under two time stamps. My log structure looks like:
[2014-04-07 23:59:58] CheckForCallAction [ERROR] Exception caught in +CheckForCallAction :: null
--Checking user--
Post
[2014-04-08 00:00:03] MobileAppRequestFilter [DEBUG] Action requested checkforcall
Suppose I want to extract everything between 2014-04-07 23:00
and 2014-04-08 02:00
.
Please note the start time stamp or end time stamp may not be there in the log, but I want every line between these two time stamps.
Best Answer
You can use
awk
for this:Where:
-F
specifies the characters[
and]
as field separators using a regular expression$0
references a complete line$2
references the date fieldp
is used as boolean variable that guards the actual printing$0 ~ /regex/
is true if regex matches$0
>=
is used for lexicographically comparing string (equivalent to e.g.strcmp()
)Variations
The above command line implements right-open time interval matching. To get closed interval semantics just increment your right date, e.g.:
In case you want to match timestamps in another format you have to modify the
$0 ~ /^\[/
sub-expression. Note that it used to ignore lines without any timestamps from print on/off logic.For example for a timestamp format like
YYYY-MM-DD HH24:MI:SS
(without[]
braces) you could modify the command like this:(note that also the field separator is changed - to blank/non-blank transition, the default)