perl_geoff has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a program that grabs a log file and parses it for errors. I am curious as to how to use a regular expression to grab only the most current log file...for example if my log files contain the date in their name like 'logfilename_20061201_LOG', I would like to make my program only grab the log with the date string greater than or equal to the current date. I was thinking of using a regex for the logfilename string, but not sure how to write it (still new to regex.)

My second question: Is there a way to then take the system date, convert it into a numerical format like 20061201 and then say "only open the log file that matches the log filename with the date greater than or equal to the system date in numerical format like 20061201?" My pseudocode:

my $logpath = "\\\\directory\\log\\"; my $logfile = "logfilename_20061201_LOG"; my $date = "20061201"; my $log = "$logpath$logfile"; open LOG, "<", $log or die "Cannot open $log for read :$!"; while (<LOG>) { if ($log =~ /$dateregex/) { etc...
Any ideas? I'm still hacking around, but this would be really ideal for my program. Am I on the right track? Thanks ^^

Replies are listed 'Best First'.
Re: regular expression for log file date ~ questions and ideas ~
by MonkE (Hermit) on Dec 01, 2006 at 18:57 UTC
    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";
      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
Re: regular expression for log file date ~ questions and ideas ~
by ww (Archbishop) on Dec 01, 2006 at 18:43 UTC
    Suggest a bit of study before hacking into the jungle.
    for example, you might wish to read about globs and the various date modules.
    <GRIN>
    As as for the objective in this part of your opening graph:
    "...grab the log with the date string greater than ... the current date..."
    please read up on:
    • PSI::xxx
    • TIMEMACHINE:xxx....
Re: regular expression for log file date ~ questions and ideas ~
by Fletch (Bishop) on Dec 01, 2006 at 18:54 UTC

    /boggle

    A date greater than the current date? Are you trying to parse logfiles from your TARDIS? Or extract information from a chip found in a crushed terminator?

Re: regular expression for log file date ~ questions and ideas ~
by graff (Chancellor) on Dec 02, 2006 at 17:17 UTC
    Is there a way to then take the system date, convert it into a numerical format like 20061201...?

    I've become quite fond of POSIX::strftime for this sort of thing:

    use POSIX; my $ymdstring = strftime( "%Y%m%d", localtime ); print "looking for log files named *$ymdstring* ...\n"; my @files = <*$ymdstring*>; print join "\n", @files, "";

    And of course there are plenty of date-related CPAN modules to supplement the standard modules (such as Time::Local and POSIX) that come with perl.