The moose likes Linux / UNIX and the fly likes Log between two Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Log between two Time" Watch "Log between two Time" New topic

Log between two Time

nazzy khan
Ranch Hand

Joined: May 06, 2010
Posts: 39


How do I grep log between two times.

Say, I want logs between 18 Feb 2011 12:40 and 18 Feb 2011 22:28

I have tried this by egrep, sed and awk but could not get the right logs.

awk '/18\/Feb\/2011:12:40:/, /18\/Feb\/2011:22:28:/' app_server.log | grep -i "error"

It gives me error says... awk: record `2011-02-18 00:00:15,...' too long

Any idea how to get logs between two timestamp...


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Grep doesn't parse timestamps, and neither will your AWK script. You're going to need something more intelligent.

Some Linux distros came with a log viewer app that might be able to help, but I don't know what's available at the moment. I think the viewer may have been named something like "sawmill".

Failing that, you could write a perl or python script that parsed the dates and filtered lines. Or, for that matter, a Java Application using the DateParser class. Probably doable in about 100 lines of Java.

AWK might be able to handle the date parsing, too, but it would be a lot of work, I think.

An IDE is no substitute for an Intelligent Developer.
nazzy khan
Ranch Hand

Joined: May 06, 2010
Posts: 39

Thanks Tim for giving some suggestions...

I'll look into that and will let you know

Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Maybe I didn't get the question right, but this works for me:

Yes, you have to know in advance whether those timestamps are present in the logfile, because the patterns aren't regarded as date/time, but simply as strings. Filter from this to that string.
Krzysztof Otrebski

Joined: Jun 06, 2012
Posts: 4
I'm using OtrosLogViewer with timefilter. This is a Swing desktop application, but it can open files from SSH/SFTP servers.
I agree. Here's the link:
subject: Log between two Time
It's not a secret anymore!