vxp has asked for the wisdom of the Perl Monks concerning the following question:
Here's the thing:
I've a log file. I need to count how many times a particular string appeared in that log file within the past 1 hour.
So I employed File::ReadBackwards for that purpose.
However, I get very weird results!
Judge for yourself, code (and output from it) below
#!/usr/bin/perl # Checks for proactive hits within the past hour use File::ReadBackwards; use POSIX "strftime"; my $success; my $items; my $earliest = POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime( time() + - 60 * 60 ) ); print "$earliest\n"; $bw = File::ReadBackwards->new( '/software/logs/CCASL/ccasl.log' ) or die "can't read '/software/logs/CCASL/ccasl.lo +g' $!" ; while ( defined( my $log_line = $bw->readline() ) ) { last if $log_line lt $earliest; if ($log_line =~ /ProcessHTTPRequestImpl/) { print $log_line; } }
Here's the output you get when you run it:
[root@lpo-tomcat-09 ~]# prohits.pl 2007-07-10 12:17:22 2007-07-10 13:17:05,879 (rocessor23) INFO [ProcessHTTPRequestImpl] Ca +llOut Params: DREREFERENCE:6272332 catTag:10006 caller:rtr2_CAP1_1a w +arp:true 2007-07-10 13:17:05,750 (rocessor16) INFO [ProcessHTTPRequestImpl] Ca +llOut Params: DREREFERENCE:6272334 catTag:10006 caller:rtp2_CAP3_1a w +arp:true 2007-07-10 13:17:05,742 (rocessor24) INFO [ProcessHTTPRequestImpl] Ca +llOut Params: DREREFERENCE:6272326 catTag:10006 caller:rtr2_CAP1_1a w +arp:true 2007-07-10 13:17:05,627 (rocessor16) INFO [ProcessHTTPRequestImpl] Ca +llOut Params: DREREFERENCE:6272259 catTag:10006 caller:rtp2_CAP4_1a w +arp:true [root@lpo-tomcat-09 ~]#
As you see, $earliest is set to the right string - one hour ago. But it only prints stuff that happened within the last minute, not last hour!
Any ideas?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem using File::ReadBackwards
by ysth (Canon) on Jul 10, 2007 at 17:34 UTC | |
|
Re: Problem using File::ReadBackwards
by thezip (Vicar) on Jul 10, 2007 at 17:46 UTC | |
by vxp (Pilgrim) on Jul 10, 2007 at 17:49 UTC | |
by rpanman (Scribe) on Jul 10, 2007 at 18:41 UTC | |
by ysth (Canon) on Jul 10, 2007 at 17:51 UTC | |
|
Re: Problem using File::ReadBackwards
by ikegami (Patriarch) on Jul 10, 2007 at 17:32 UTC | |
by vxp (Pilgrim) on Jul 10, 2007 at 17:35 UTC | |
by ikegami (Patriarch) on Jul 10, 2007 at 17:40 UTC | |
by ysth (Canon) on Jul 10, 2007 at 17:43 UTC |