From: Reid Thompson Date: 2012-01-20T22:58:47+09:00 Subject: Re: Parsing log with date time entry On Tue, 2012-01-17 at 07:56 +0900, Christopher Graves wrote: > The log file looks like this > > 01/10/2012 14:46:02:87 Some Data > 01/10/2012 14:46:03:319 Some Stuff > 01/10/2012 14:46:04:160 OMFG > 01/10/2012 14:46:04:160 okay > > I have the script to the point where it grabs all the logs, sorts > through them and spits out the lines with the specified date/time into a > separate file. > > What would be the best way to get the current time formatted per the log > entries and collect a range of times say from current to 15 minutes > previously. > $ cat test.log 01/10/2012 14:46:02:87 Some Data 01/10/2012 14:46:03:319 Some Stuff 01/10/2012 14:46:04:160 OMFG 01/10/2012 14:46:04:160 okay # assume format is m/d/y require 'rio' def test now = Time.utc(2012, 1, 10, 14, 59, 00) puts "Explicitely set now to " + now.to_s puts "" rio('/home/rthompso/test.log').lines { |line| dt, tm, msg = line.split # assume format is m/d/y month, day, year = dt.split('/') hr, min, secfrac = tm.split(':') ts = year +'-' + month + '-' + day + ' ' + tm at = Time.utc(year, month, day, hr, min, secfrac) p at cutoff = (now - (15 * 60)) puts "15 mins ago was " + (now - (15 * 60)).to_s if ( at >= Time.at(cutoff) ) puts line end } #f = rio('/home/rthompso/test.log') > ?- puts "" puts "" puts "" now = Time.utc(2012, 1, 10, 15, 01, 5) puts "Explicitely set now to " + now.to_s puts "" rio('/home/rthompso/test.log').lines { |line| dt, tm, msg = line.split # assume format is m/d/y month, day, year = dt.split('/') hr, min, secfrac = tm.split(':') ts = year +'-' + month + '-' + day + ' ' + tm at = Time.utc(year, month, day, hr, min, secfrac) p at cutoff = (now - (15 * 60)) puts "15 mins ago was " + (now - (15 * 60)).to_s if ( at >= Time.at(cutoff) ) puts line end } end irb(main):089:0> test Explicitly set now to Tue Jan 10 14:59:00 UTC 2012 Tue Jan 10 14:46:02 UTC 2012 15 mins ago was Tue Jan 10 14:44:00 UTC 2012 01/10/2012 14:46:02:87 Some Data Tue Jan 10 14:46:03 UTC 2012 15 mins ago was Tue Jan 10 14:44:00 UTC 2012 01/10/2012 14:46:03:319 Some Stuff Tue Jan 10 14:46:04 UTC 2012 15 mins ago was Tue Jan 10 14:44:00 UTC 2012 01/10/2012 14:46:04:160 OMFG Tue Jan 10 14:46:04 UTC 2012 15 mins ago was Tue Jan 10 14:44:00 UTC 2012 01/10/2012 14:46:04:160 okay Explicitly set now to Tue Jan 10 15:01:05 UTC 2012 Tue Jan 10 14:46:02 UTC 2012 15 mins ago was Tue Jan 10 14:46:05 UTC 2012 Tue Jan 10 14:46:03 UTC 2012 15 mins ago was Tue Jan 10 14:46:05 UTC 2012 Tue Jan 10 14:46:04 UTC 2012 15 mins ago was Tue Jan 10 14:46:05 UTC 2012 Tue Jan 10 14:46:04 UTC 2012 15 mins ago was Tue Jan 10 14:46:05 UTC 2012