First perlmonks post. If you have very large log files or slow disk, or both... As long as the log file is ordered by datetime, you can use Search::Dict's binary search to find entries for datetimes. (this code has not been extensively tested)
#! perl -W use strict; use warnings; # Yes, I know it's big and slow, room for improvment here use Date::Manip; use Search::Dict; # The log file must be ordered by date for this to work. # Use it like this: # search_log /var/log/httpd/access_log "2008052205:32:43" # or # search_log /var/log/httpd/access_log "2008052205:32:4" # search_log /var/log/httpd/access_log "2008052205:32:" # search_log /var/log/httpd/access_log "2008052205:32" # search_log /var/log/httpd/access_log "2008052205:3" my $file = shift or die 'no file'; my $search_date = shift or die 'no search date in YYYYMMDDHH::MM::SS f +ormat'; open my $fh,'<',$file or die $!; sub get_date{ my($line) = @_; my($d) = $line=~/\[(.*)\]/mxo or die "Can't find [date] in line: $ +line"; return ParseDate($d) or die "Can't parse date: $d"; } my $pos = look $fh,$search_date,{ xfrm=>*get_date, }; if(-1 == $pos){ die "Error looking for '$search_date' in file '$file': $!\n"; } while(my $line=<$fh>){ if(get_date($line) =~m/$search_date/mxo){ print $line; } else { last; } }

In reply to Using Search::Dict on log files by stigpje

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.