Grep last 2 minutes log only

I have newbie,
which i use for checking last one hours log file,
but i want to check 2 minutes log and discard old log, only match current time with last 2 minutes.

Ex log.

2018-07-03 20:09:17
2018-07-03 20:05:17
2018-07-03 20:05:18
2018-07-03 20:05:20
2018-07-03 20:06:22
2018-07-03 20:06:26
2018-07-03 20:07:26
2018-07-03 20:07:29
2018-07-03 20:07:30
2018-07-03 20:07:30
2018-07-03 20:07:31
2018-07-03 20:09:31
2018-07-03 20:09:32
2018-07-03 20:09:32
2018-07-03 20:09:33
2018-07-03 20:09:33
2018-07-03 20:09:34
2018-07-03 20:09:35
2018-07-03 20:09:36
2018-07-03 20:09:39
2018-07-03 20:09:41
2018-07-03 20:09:44
2018-07-03 20:09:45
2018-07-03 20:09:47
2018-07-03 20:09:48
2018-07-03 20:09:49
2018-07-03 20:09:49
2018-07-03 20:09:50
2018-07-03 20:09:50
2018-07-03 20:09:50
2018-07-03 20:09:51
2018-07-03 20:09:52
2018-07-03 20:09:52
2018-07-03 20:09:53
2018-07-03 20:09:53
2018-07-03 20:09:54
2018-07-03 20:09:54
2018-07-03 20:09:56
2018-07-03 20:10:00
2018-07-03 20:10:03
2018-07-03 20:10:04
2018-07-03 20:10:06
2018-07-03 20:10:08
2018-07-03 20:10:13
2018-07-03 20:10:13
2018-07-03 20:10:15
2018-07-03 20:10:15
2018-07-03 20:10:17
2018-07-03 20:10:21
2018-07-03 20:10:22
2018-07-03 20:10:22
2018-07-03 20:10:23
2018-07-03 20:10:23
2018-07-03 20:10:24
2018-07-03 20:10:24
2018-07-03 20:10:25
2018-07-03 20:10:25
2018-07-03 20:10:26
2018-07-03 20:10:26
2018-07-03 20:10:27
2018-07-03 20:10:28
2018-07-03 20:10:29
2018-07-03 20:10:29
2018-07-03 20:10:30
2018-07-03 20:10:31
2018-07-03 20:10:31
2018-07-03 20:10:32
2018-07-03 20:10:35
2018-07-03 20:10:39
2018-07-03 20:10:39
2018-07-03 20:10:39
2018-07-03 20:10:40
2018-07-03 20:10:41
2018-07-03 20:10:41
2018-07-03 20:10:43
2018-07-03 20:10:44
2018-07-03 20:10:46
2018-07-03 20:10:49
2018-07-03 20:10:50
2018-07-03 20:10:50
2018-07-03 20:10:53
2018-07-03 20:10:55
2018-07-03 20:10:56
2018-07-03 20:10:57
2018-07-03 20:10:57
2018-07-03 20:10:59
2018-07-03 20:10:59
2018-07-03 20:11:00
2018-07-03 20:11:00
2018-07-03 20:11:06
2018-07-03 20:11:08
2018-07-03 20:11:10
2018-07-03 20:11:11
2018-07-03 20:11:12
2018-07-03 20:11:14
2018-07-03 20:11:15
2018-07-03 20:11:35
2018-07-03 20:11:39
2018-07-03 20:11:58
2018-07-03 20:11:59

i want command to grep log only 2 min from last line.
example 2018-07-03 20:10:00 - 2018-07-03 20:11:59

Hi,
Maybe as similar request: Grep last 30 minutes log only

Regards.

1 Like

Not work on sunos

bash-3.2$ date --date='30 minutes ago' '+%b %_d %H:%M'
date: illegal option -- date=30 minutes ago
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
bash-3.2$ 
bash-3.2$ uname
SunOS
bash-3.2$ 

Very simple, just bash arithmetics, no date functionality, doesn't cross hour nor midnight boundaries, relies on only full minutes being used / searched:

TMP=$(tail -1 file)
TMP=${TMP%:*}
MIN=${TMP#*:}
grep  "${TMP/$MIN/\\($((MIN-1))\\|$MIN\\)}" file

If your grep allows for extended regexes (-E option), drop the double backslashes.

........

@RudiC: your solution not work with by example: 2018-07-03 20:00:00

Another solution (with perl) :

LOGFILE=/tmp/foobar.log
read a b < <(tail -1 "$LOGFILE" | perl -ne 'use POSIX;  ($Z1,$Z2,$Z3,$Z4,$Z5)= $_=~ /^(....).(..).(..).(..).(..)/ ; print "$Z1$Z2$Z3$Z4$Z5 ",strftime "%Y%m%d%H%M\n", localtime(mktime( 0, $Z5, $Z4, $Z3, $Z2-1, $Z1-1900 )-120);')
perl -ne '($Z1,$Z2,$Z3,$Z4,$Z5)= $_ =~ /^(....).(..).(..).(..).(..)/; print $_ if ("$Z1$Z2$Z3$Z4$Z5"  >= '$b' and "$Z1$Z2$Z3$Z4$Z5" <= '$a')' "$LOGFILE"

Regards.

1 Like