I have a log file that contains a lot of information and I would like to only parse the contents of that file which fall within the last 24 hours
Each line in the file begins with a timestamp such as Jan 31 13:13:02
and then has a log message.
I currently have a batch file that finds the start and end time like this
start=$(date +"%b %d %H:%M:%S" --date="-1 day")
end=$(date +"%b %d %H:%M:%S")
I was then hoping to use these times along with a grep -c "data_to_find"
to find the number of occurrences of a certain log message so that I can then act on this later.
In short, How can I take into account the times and then grep the content for the number of occurrences of a string within said file?
I am on a linux system and have no issue with any solution that uses SED, AWK, GREP etc.
Not so simple without writing a shell script (especially if it's not sorted).
I would try something like this to get all the lines between 1 day ago and now (interpolate as needed), and then
grep -c
pipe whatever you want from output. Note below assumes date format is something likeJan 31 13:13:02
(2 spaces between Month and Day)This can be tricky because the time with the exact second you are looking for may not exist if there was no log entry at that time.
Another possibility may be to add a marker to the log each time you read it and then just look at entries after your last marker. So, when you have read the log, let's say it is called "log.txt" you could do:
Then, when you want to read the log starting from your last marker, you can find the last marker like this:
and read everything after it like this:
Awk could certainly do that in one script. In sed, counting is a bit complex so
wc
is used (a bit faster thansed -n '$ ='
). Yourpattern is a regex that suite to your log message to find