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

Hi Monks,
I need a little help here please, I had a similar code before, but I can't find it anymore. All I would like to do is, having a program that will go through all my directories in my server looking for a file called myerror.txt and do a check like, if the date found is older than a month from today's date, I need to compress this file and clear it's content up to the month old date. Can someone help me with that ?
Heis a sample of this log file:
[Mon oct 19 10:33:31 2005] par.pl: Undefined subroutine CGI::one_min [Fri nov 19 03:05:23 2005] par.pl: Undefined subroutine CGI::one_min2 [Thu Jan 19 09:45:39 2006] test.pl: Undefined subroutine CGI::end_test

Thanks a lot!

Replies are listed 'Best First'.
Re: Find and Compressing log files.
by VSarkiss (Monsignor) on Jan 26, 2006 at 16:17 UTC
      Thank you!
Re: Find and Compressing log files.
by EvanK (Chaplain) on Jan 26, 2006 at 16:18 UTC
    well, you'd probably want to read in the log file as an array:
    open(ERRLOG,"myerror.txt"); @file = <ERRLOG>;
    then loop through the resultant array, using something like Date::Parse to convert the dates to unix time (ex: "Thu Jan 26 10:04:56 2006" becomes 1138291496), and splice off any elements older than the given time:
    foreach $line (0..$#file) { # grab the date and dump into $date $date = $1 if $file[line] =~ m/^\[(.*?)\]/; # parse it to unix time $date = str2time($date); # method from Date::Parse # if its older than a month (30 days specifically) if($date < (time-2592000)) # 2592000 secs = 30 days { splice @file,$line,1; } }
    then just output the array (whats left of it, anyway) back to the file. Note that this code hasnt been tested, by the way.

    __________
    Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
    - Terry Pratchett