I need to find a way for grep
to return different line sized contents after a match. Some matches have 4 lines after them, other have 20. I need them all.
Every hour I am looking to return the previous hour's log entries, which have timestamps like:
time: 20200115132443
To do this, I imagine every hour a cronjob will run the previous hour's timestamp as a grep statement:
grep "time: 2020011507"
The problem I'm having is the log has different sizes for certain entries after a match, so just things like grep -C 5
won't really work all the time.
I'm wondering if I should either be returning all contents of a match up to the next matched grep statement (which would miss the last entry) or if there is some other way to run this. A sample log file snippet looks like:
time: 20200115132443
logging stuffs
time: 20200115134543
more logging
stuffs
like this
time: 20200115140201 # this should now not be returned
Maybe I am overcomplicating and can just run a grep
statement of the current hour and just grab everything before that hour and after the hour previous.
awk -v t="20200115134543" '/^time:/{if($2 == t) p=1; else p=0} p' file.log