On May 20, 4:37 am, cmksw...@gmail.com (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: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to