in reply to search and replace last line

Depends if the file is small enough to fit into memory, if it is here ya go..

-Waswas
#!/usr/local/bin/perl my @fileslurp = (); my $datefld = ""; my $testdate = "200208"; my $newline = "Not the old date\n"; my $thisfile = "/var/tmp/junk.log"; open (FILE,"$thisfile"); @fileslurp = <FILE>; close FILE; ($datefld) = split(/ /,@fileslurp[$#fileslurp]); @fileslurp[$#fileslurp] = $newline if ($datefld eq "$testdate") ; foreach $x (@fileslurp) { print $x; }

Replies are listed 'Best First'.
Re: Re: search and replace last line
by Anonymous Monk on Aug 07, 2002 at 15:56 UTC
    thanks....what is the easiest way to replace the last line of the file?
      If you don't want to do the traditional approach of buffering and re-writing the data yourself, you can try Tie::File. A small example using your test data:

      #!/usr/bin/perl -wd use Tie::File; tie @array, 'Tie::File', "test.txt" or die "cannot tie file\n"; ($date,$data) = split( /\s+/, $array[-1] ); $data = "whatever" if $date eq "200208"; $array[-1] = "$date $data"; untie @array;

      -derby