Weblog entry #3 for PJ_at_Belzabar_Software
Imagine you have a log file you want to grep through daily to look at what went on yesterday. The log file is timestamped with the date, and is in a human-readable type of format (eg starting with: Sep 5 2007 18:53). How do you grep through yesterday's logs in an automated way without putting in yesterday's date manually? And keeping it simple?
The problem is that yesterday isn't defined with simple code (think of the rollover for a month or year etc). Which is why we have a zillion perl date handling modules.
But we can avoid worrying about it. The principle is to define yesterday using something like this in bash:
yesterday=`perl -e '$string = localtime($ARGV[0]-86400); print "$string"' \`date +%s\``
(the way this works is that bash's date gives today's date. This is output in the seconds-since-1970 format. This output is the argument used by perl, and has 86400 seconds (1 day) chopped off it. The string that perl then prints out is a human readable timestamp format (eg Wed Sep 5 13:59:57 2007) and this is what becomes the $yesterday variable.)
(Oh, yeah, and you may want to add a
| cut -b5-10
or something like that before the last backtick if you want to handle only some fields).
Then you grep using $yesterday as your pattern through the log file.
The reason I think this is cool is because of the way you dump the problem onto bash date and perl, which handle dates robustly, so the problem is no longer yours.
Well, I thought it was frightfully neat. Now I'm off to pat my back.
PJ
Comments on this Entry
[ Send Message | View Weblogs ]
date --date=yesterday
This doesn't seem to be documented on my u****u dapper system though.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Send Message | View Weblogs ]
Yes, indeed, info date had it well-documented. An eye-opener on how clear and readable documentation can be actually - it makes me want to curl up and read it this evening. OK, so that's the geek in me ;-)
Other OSs may have non-GNU date utilities, so this little perl/bash snippet is probably still useful elsewhere too, seeing how ubiquitous perl/bash are.
[ Parent | Reply to this comment ]
[ Send Message | View dkg's Scratchpad | View Weblogs ]
[0 dkg@squeak ~]$ date --date='3 weeks ago next Wednesday' Wed Aug 22 00:00:00 EDT 2007 [0 dkg@squeak ~]$
[ Parent | Reply to this comment ]
Only those daft enough [;-)] to invent their own log files, and own rotation method, have this specific problem (especially if they forgot to grep before they ran compress!). Although {Bourne|Korn|Posix|Bash} shell should have had date handling routines built in.
[ Parent | Reply to this comment ]
Thanks for reminding us about logcheck, which is the properly sophisticated way to do it (it detects anomalies).
For my purpose I wasn't interested in anomolies, but frequency of an entry yesterday.
PJ
[ Parent | Reply to this comment ]