in reply to regular expression for log file date ~ questions and ideas ~

How about simply choosing the newest log file. Unless you plan on hosting time travellers who make log entries from the future, the current log will always be the newest. I've included a bit of code below that will choose the newest log file from a particular directory based soley upon the filename. It assumes that the log filenames are of the form: *YYYYMMDD*_LOG
use strict; use warnings; my $LOG_DIR = '.'; opendir DH, $LOG_DIR or die; my @files = sort grep { /\_LOG$/i } readdir DH; close DH; my $lastlog = pop @files; print "lastlog=$lastlog\n";

Replies are listed 'Best First'.
Re^2: regular expression for log file date ~ questions and ideas ~
by perl_geoff (Acolyte) on Dec 01, 2006 at 19:19 UTC
    Thanks for the code...sorry about the slipup; I did mean equal to *not* greater than or equal to LOL. Now, I tried your code and it seems to work but what if my logfile was named something like filename-061201-randomnum-1293_log where the date is a bit before the _log and also some of my log names are similar but end in .log instead of _LOG. Anyway, your code got me thinking at least, thanks.
      Only you know what randomnum is, we can only assume it will be random and unpredictable. :)
      my $randomNum = getMyRandomNum(); my $directory = "/path/to/some/dir"; openddir DIR, $directory or die "opening $directory failed"; my $specialRegex = qr/$randomNum.*log$/i; my @logfiles = grep /$specialRegex/, readdir DIR; for my $logfile (@logfiles) { open LOG "<", $logfile or warn "...."; while (<LOG>) { print if dateMatches() .. 0; # should print all lines from where d +ateMatches() returns TRUE. ... } }


      perl -e '$,=$",$_=(split/\W/,$^X)[y[eval]]]+--$_],print+just,another,split,hack'er
        hmmm...where is your subroutine getMyRandomNum? Aside from that, some of your ideas look pretty helpful.