From: Peter Vandenabeele Date: 2012-01-22T01:49:01+09:00 Subject: Re: Parsing log with date time entry --00235429e27c3b74fc04b70c9260 Content-Type: text/plain; charset=UTF-8 On Fri, Jan 20, 2012 at 8:21 PM, Christopher Graves wrote: > The Rio way looks interesting but even after installing the gem it is > not working right on my test log. > > I did manage to get it working > > require 'time' > > s_time = (Time.now - (15*60)) > e_time = Time.now > range = > > (s_time.strftime("%m/%d/%Y\s%H:%M:%S:%L")..e_time.strftime("%m/%d/%Y\s%H:%M:%S:%L")) > Dir["*.log"].each {|path| > File.readlines( path ).each do |sort| > sort.scan(/^\d{2}\/\d{2}\/\d{4}\s\d{1,2}\:\d{2}\:\d{2}\:\d{1,3}/).each > do |time| > > if range.cover? time > puts sort > end > end > end > > } > > What do you guys think? > I am afraid this version will fail to select any line for the first 15 minutes of every year. But maybe that is not an issue? 021:0> e_time = Time.new(2012,1,1,0,0,30) => 2012-01-01 00:00:30 +0100 022:0> s_time = e_time-900 => 2011-12-31 23:45:30 +0100 023:0> range = s_time.strftime("%m/%d/%Y\s%H:%M:%S:%L")..e_time.strftime("%m/%d/%Y\s%H:%M:%S:%L") => "12/31/2011 23:45:30:000".."01/01/2012 00:00:30:000" 027:0> log_line = "01/01/2012 00:00:20:000 Some Data" => "01/01/2012 00:00:20:000" 028:0> range.cover? log_line => false I wrote a version where the string comparison is executed on a %Y/%d/%s ... version and that version seems to behave correctly, both for the simple case and on a newyear morning. The catch is that I swapped %m/%d with %Y before calling cover? ($1, $2 and $3 return the groups in a recent regex). # e_time = Time.new(2012,1,1,1,0,30) # the simple case e_time = Time.new(2012,1,1,0,0,30) # the year boundary case s_time = e_time - 900 range = s_time.strftime("%Y/%m/%d\s%H:%M:%S:%L")..e_time.strftime("%Y/%m/%d\s%H:%M:%S:%L") test_log_file = [ "12/31/2011 22:00:00:000 way before", "12/31/2011 23:45:30:000 just inside", "01/01/2012 00:00:20:000 Some Data just before now", "01/01/2012 00:00:40:000 Some Data just after now", "01/01/2012 01:00:20:000 An hour later"] [test_log_file].each {|path| path.each do |sort| sort.scan(/^(\d{2}\/\d{2})\/(\d{4})(\s\d{1,2}\:\d{2}\:\d{2}\:\d{1,3})/).each do time = "#{$2}/#{$1}#{$3}" if range.cover? time puts sort end end end } HTH, Peter --00235429e27c3b74fc04b70c9260--