in reply to Removing lines from files
with:tie my @file, 'Tie::File', $File::Find::name or die "Can't + tie $File::Find::name $!"; splice @file, 0, 4; untie @file;
The only caveat is that you have to ensure that $tmp can never be the name of an existing log file.open(F, '<', $File::Find::name) or die "..."; my $tmp = $File::Find::name . " - new"; # see comment below open(G, '>', $tmp) or die "..."; for (1..4) { <F> }; # don't copy the first four lines while (<F>) { print G } close(G); close(F); rename($tmp, $File::Find::name) or warn "unable to replace $File::Find::name: $!\n";
The tie method is inefficient because it reads the entire file into memory.
One advantage that this approach has over an in-place re-write is that you won't have to worry about leaving yourself with a corrupted log file if the copy is interrupted.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Removing lines from files
by Cristoforo (Curate) on May 09, 2008 at 20:09 UTC |