Well, I think I figured it out.
When I reviewed the examples in Perl Cookbook more closely I realized that
the array has to be big enough to hold all the lines that go back into the
existing file.  If we run our script shortly after midnight, parsing for
date, most of the lines will go to the .old file.  The current day's lines
can be written back into the existing file (after we're done processing the
whole thing) by using the seek and truncate functions.

Here's the new code snip if anybody has any more words of wisdom:

copy("$log","$log.backup");             # make backup
$logold = "$log.old";
open (LOGOLD, ">$logold") or die ("Cannot open $logold \n");
open(LOG, "+< $log");
$out = '';
while (<LOG>) {
    if  ($todayfound eq "y") {         # if today's date has already been
found
    $out .= $_;                      # write to .out to go back in original
$log  
    } elsif (m/($weekday)\s+           # match for today's weekday, spa
            ($month)\s+                #       and today's month, spaces
            (\d+)/x                    #       and one or more digits
      and $3 == $day){                 #       and digits are day of month
      $todayfound="y";                 #       then set flag for date found
      $out .= $_;                      # write to .out to go back in
original $log
    } else {                           # if we have not reached today's date
      print LOGOLD;                    # write the line to the LOGOLD file
      #s#.*\n##;                       # do not put line into $out
    }
  }
close (LOGOLD);
seek(LOG, 0, 0)          or die "can't seek to start of $log: $!";
print LOG $out           or die "can't print to $log: $!";
truncate(LOG, tell(LOG))  or die "Can't truncate $log: $!";
close (LOG)              or die "can't close $log: $!";

As it says in the cookbook... "this approach is for the truly determined".

Thanks again,
Beth

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to