On May 20, 4:37 am, [email protected] (Ambuli) wrote:
> Here i paste a perl script to delete last Two Lines. If you want
> delete more lines in a file you can specify it.
>
> use File::ReadBackwards;
> my $filename = 'test.txt';
> my $Lines_to_truncate = 2; # Here the line to truncate is mean Remove
> only Last Two Lines
> my $bw = File::ReadBackwards->new( $filename )
> or die "Could not read backwards in [$filename]: $!";
> my $lines_from_end = 0;
> until( $bw->eof or $lines_from_end == $Lines_to_truncate )
> {
> print "Got: ", $bw->readline;
> $lines_from_end++;
> }
> truncate( $filename, $bw->tell );
Although tie'ing is slow, the core module Tie::File provides
an easier way:
use Tie::File;
tie my @array, 'Tie::File', '/path/to/somefile' or die ...;
$#array -= 2; # chop two records off the end
--
Charles DeRykus
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/