This question already has an answer here:
Trying to find a simple way for watching for recent events (from less than 10 minutes), I've tried this:
awk "/^$(date --date="-10 min" "+%b %_d %H:%M")/{p++} p" /root/test.txt
but it doesn't work as expected...
Log files are in form :
Dec 18 09:48:54 Blah
Dec 18 09:54:47 blah bla
Dec 18 09:55:33 sds
Dec 18 09:55:38 sds
Dec 18 09:57:58 sa
Dec 18 09:58:10 And so on...
A Ruby solution (tested on ruby 1.9.3)
You can pass days, hours, minutes or seconds as a parameter and it will search for the expression and on the file specified (or directory, in which case it will append '/*' to the name):
In your case just call the script like so: $0 -m 10 "expression" log_file
Note: Also if you know the location of 'ruby' change the shebang (first line of the script), for security reasons.
That's a (common) job for perl!:
Simple and efficient:
This version print last 10 minutes event, upto now, by using
time
function.You could test this with:
Note that first representation use only firsts 15 chars from each lines, while second construct use more detailed regexp.
As a perl script:
last10m.pl
Strictly: extract last 10 minutes from logfile
Meaning not relative to current time, but to last entry in logfile:
There is two way for retrieving end of period:
Where logically, last modification time of the logfile must be the time of the last entry.
So the command could become:
or you could take the last entry as reference:
Second version seem stronger, but access to file only once.
As a perl script, this could look like:
But if you really wanna use bash
There is a very quick pure bash script:
Warning: This use recent bashisms, require
$BASH_VERSION
4.2 or higher.Store this script and run:
Strictly: extract last 10 minutes from logfile
Simply replace line 10, but you have to place filename in the script and not use it as a filter:
You can match the date range using simple string comparison, for example:
For example if
d1='Dec 18 10:19'
andd2='Dec 18 10:27'
then the output will be:Or using
awk
if you wish:In bash, you can use the
date
command to parse the timestamps. The "%s" format specifier converts the given date to the number of seconds since 1970-01-01 00:00:00 UTC. This simple integer is easy and accurate to do basic arithmetic on.If you want the log messages from the last 10 minutes of actual time:
Note the
${line:0:15}
expression is a bash parameter expansion which gives the first 15 characters of the line, i.e. the timestamp itself.If you want the log messages from the last 10 minutes relative to the end of the log:
Here's a mild performance enhancement over the above:
This assumes the log entries are in strict chronological order. Once we match the timestamp in question, we exit the for loop, and then just use
cat
to dump the remaining entries.Here is nice tool range is any you wish from -10 till now
In python, you could do as follows:
Put the lines from the file into a stack (a python list). pop the last item and get difference between the successive date items until you get the difference as less than 600 seconds.
Running on your input, I get the following: