Truncate Last few lines

Truncate Last few lines

am 20.05.2011 13:37:52 von muthukumar swamy

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 );


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Truncate Last few lines

am 20.05.2011 15:46:30 von Shlomi Fish

Hi Ambuli,

a few comments on your code:

On Friday 20 May 2011 14:37:52 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.
>
>

Always start with "use strict;" and "use warnings".

> use File::ReadBackwards;

Include some empty lines between logical paragraphs of your code.

> my $filename = 'test.txt';

This is better specified as a command line argument
> my $Lines_to_truncate = 2; # Here the line to truncate is mean Remove
> only Last Two Lines

The style of your variable name is very strange. It should be:

[code]
my $num_lines_to_truncate = 2;
[/code]

Also consider specifying it using Getopt::Long.

> my $bw = File::ReadBackwards->new( $filename )
> or die "Could not read backwards in [$filename]: $!";

The "or die" should be indented.

> my $lines_from_end = 0;
> until( $bw->eof or $lines_from_end == $Lines_to_truncate )
> {
> print "Got: ", $bw->readline;
> $lines_from_end++;
> }

The print here is redundant and only adds noise and clutter to the output.

> truncate( $filename, $bw->tell );

It would be safer to keep track of the position, then destroy $bw, and only
then truncate the file.

Regards,

Shlomi Fish

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Understand what Open Source is - http://shlom.in/oss-fs

Beliefs are what divide people. Doubt unites them.
-- http://en.wikiquote.org/wiki/Peter_Ustinov

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Truncate Last few lines

am 20.05.2011 18:03:06 von derykus

On May 20, 4:37=A0am, 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;
> =A0my $filename =3D 'test.txt';
> =A0my $Lines_to_truncate =3D 2; # Here the line to truncate is mean Remov=
e
> only Last Two Lines
> =A0my $bw =3D File::ReadBackwards->new( $filename )
> or die "Could not read backwards in [$filename]: $!";
> my $lines_from_end =3D 0;
> =A0until( $bw->eof or $lines_from_end == $Lines_to_truncate )
> =A0{
> =A0 =A0 =A0 =A0 print "Got: ", $bw->readline;
> =A0 =A0 =A0 =A0 $lines_from_end++;
> =A0}
> 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 -=3D 2; # chop two records off the end

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/