-1

I got the following format ,

2015-04-12 11:22:04,876 - Logs - Error: OSError(16, 'Error in NAS')
2015-04-12 11:37:37,242 - Logs - Error: OSError(16, 'Error in NAS')

I want to get only last 10 mints lines when and search for "OSError(16, 'Error in NAS')"

I cant grep and for the error but couldn't implement dates and timing for grep

any advise

Jecki
  • 802
  • 3
  • 16
  • 32
  • Similar question [here](http://stackoverflow.com/questions/25193654/grep-for-timestamp-and-word)... even better check [this](http://superuser.com/a/439695/238667) `sed` solution. – plesiv Apr 12 '15 at 08:32
  • I adjust and come up with today only lines , grep $(date +"%Y-%m-%d") /var/log/log.log |grep 'OSError' , how i can adjust to get only last 10 mints logs – Jecki Apr 12 '15 at 08:45
  • possible duplicate of [Get/extract the data from log file of last 3 minutes?](http://stackoverflow.com/questions/21042534/get-extract-the-data-from-log-file-of-last-3-minutes) – Cyrus Apr 12 '15 at 09:00

2 Answers2

2

I used python :

with open('access.log') as f:
    for line in f:
         logdate = datetime.strptime(line.split(',')[0], '%Y-%m-%d %H:%M:%S')
         if logdate >= datetime.now() - timedelta(minutes=10):
              print(line) 
Jecki
  • 802
  • 3
  • 16
  • 32
1

To filter the lines with the desired log times you can use:

perl -mTime::Piece=:override -F, -lane 'my $now = gmtime();
   print if $now - Time::Piece->strptime("$F[0]", "%Y-%m-%d %H:%M:%S")< 600' input-file

This uses Time::Piece->strptime to parse the time stamp into a usable Time::Piece object which supports comparison. By importing :override, the call to gmtime returns a Time::Piece object so that the subtraction can be made and compared to 600 (the number of seconds in 10 minutes). To further restrict output to lines which match a pattern, you can simply add && m/pattern/ to the condition, or pipe the output to a follow-on process.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted. Despite the somewhat higher tolerance for line noise in shell questions, this could use some clarification. – Nathan Tuggy Apr 14 '15 at 01:33