<?xml version="1.0" encoding="windows-1252"?>
<node id="985408" title="Re: Using binary search to get the last 15 minutes of httpd access log" created="2012-08-04 05:04:03" updated="2012-08-04 05:04:03">
<type id="11">
note</type>
<author id="733061">
flexvault</author>
<data>
<field name="doctext">
&lt;p&gt;&lt;a href="http://www.perlmonks.org/?node_id=265997"&gt;mhearse&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;If you are doing this once, then reading the whole file is fine, but if every 'nn' minutes you want to run the script then saving the location of the end-of-file would be a good solution. This technique is similar to 'pop-before-smtp' technique. (Example, not tested )
&lt;code&gt;
  my $File_Loc = 0; my $SaveLocFile = "./FileLoc";
  if ( -e $SaveLocFile )
  {   open( my $IN, "&lt;", $SaveLocFile ) or die "File Bad $!";
      $File_Loc = &lt; $IN &gt;; chomp ( $File_Loc );
      close $IN;
  }

  my $sz = ( -s $SaveLocFile );
  if ($sz &gt; 0)
  {  open( my $log, "&lt;", $log_file");          ## This should be log file
     if ($sz &lt; $File_Loc ) { $File_Loc = 0; }  ## New log file
     seek $log, $File_Loc, 0;                  ## Move to old end of file
     while ( &lt; $log &gt; )
     {   chomp;
         my $line = $_;
         # do work 
      }

      $File_Loc = tell $log;      ## Save current end-of-file location
      open( my $OUT,"&gt;", $SaveLocFile );
      print $OUT "$File_Loc\n";
      close $OUT;
      close $log;
    }

&lt;/code&gt;
&lt;/p&gt;&lt;p&gt;Since you will be running on a log server, you should use the same locking mechanism as the log daemon. There are some race conditions in the code, but if you save where you ended, you shouldn't miss anything. I originally wrote this in 1997, so I tried to update the technique. Looking at the original code, it isn't the way I would write it today, but it has worked on some 40 - 50 mail servers since, and I never had to rewrite it (probably should now).&lt;/p&gt;&lt;p&gt;
Hopefully you see the idea and can adapt this to your situation.

&lt;/p&gt;
&lt;p&gt;Good Luck&lt;/p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-733061"&gt;
&lt;p&gt;&lt;b&gt;"Well done is better than well said." - Benjamin Franklin&lt;/b&gt;&lt;/p&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
985296</field>
<field name="parent_node">
985296</field>
</data>
</node>
