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?


In reply to Problem using File::ReadBackwards by vxp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.