in reply to Dates! Log Files! Help!

use strict; use warnings; use Time::Local (); my $file_name = shift(@ARGV); open(LOG_FH, '<', $file_name) or die("...$/"); open(TMP_FH, '>', $file_name.'.tmp') or die("...$/"); # Might want to clear the hours, minutes and secs from $old. my $old = time - 90*24*60*60; while (<LOG_FH>) { my ($y, $m, $d) = /...(...)...(...)...(...).../; my $date = Time::Local::timelocal(0, 0, 0, $d, $m-1, $y); last if $date > $old; } do { print TMP_FH $_; } while (<LOG_FH>); close(LOG_FH); close(TMP_FH); unlink($file_name); rename($file_name.'.tmp', $file_name);

Replace ...s with appropriate code. Off the top of my head. There may be a better way. Doesn't lock. Untested.

Actually, Forget everything I said and run Logfile::Rotate as a cron job every 90 days.

Update: I forgot that you specified the date format. Time::ParseDate would probably be of more use than Time::Local.

Replies are listed 'Best First'.
Re^2: Dates! Log Files! Help!
by hmerrill (Friar) on Nov 22, 2004 at 19:12 UTC
    Nice solution!

    The only other thing that came to mind was using Date::Manip to recognize and parse the date. I haven't used it in a long time, but when I did I remember one of it's strengths is being able to recognize dates formatted in many different ways. The one drawback was that it was slow if using it to parse LOTS of dates. Just something to think about.