Re: Deleting last 10 lines of a file

2010-03-24 Thread sheela b
Thanks a lot Shlomi and Thomas.

I will try with these methods.


On Tue, Mar 23, 2010 at 7:13 PM, Shlomi Fish shlo...@iglu.org.il wrote:

 Hi Sheela,

 On Tuesday 23 Mar 2010 15:06:24 sheela b wrote:
  Hi All,
 
  How to delete last 10 lines of a file using Perl one liner?
 
  I used the following one liner to delete first 10 lines of a file,
 
  perl -i.bak -ne 'print unless 1..10' test.txt
 

 If it doesn't have to be a one-liner, you can do it efficiently using the
 following script:

 
 #!/usr/bin/perl

 use strict;
 use warnings;

 use File::ReadBackwards;
 use Getopt::Long;

 my $count;
 GetOptions(
n=i = \$count,
 );

 if (!defined($count))
 {
die Please specify -n for the line count.;
 }

 my $filename = shift;

 my $bw = File::ReadBackwards-new($filename)
or die Could not backwards-open '$filename' - $!;

 foreach my $idx (1 .. $count)
 {
$bw-readline();
 }

 my $wanted_len = $bw-tell();

 $bw-close();

 open my $truncate_fh, +, $filename
or die Could not truncate-open '$filename' - $!;

 truncate($truncate_fh, $wanted_len);

 close($truncate_fh);
 

 If you still insist on it being a one-liner, see:

 http://mail.perl.org.il/pipermail/perl/2005-August/007316.html

 You can do it as a one liner in a much less elegant way by using a cyclical
 queue, but I can't be bothered to implement it.

 Regards,

Shlomi Fish

 
  Regards
  Sheela

 --
 -
 Shlomi Fish   http://www.shlomifish.org/
 Parody on The Fountainhead - http://shlom.in/towtf

 Deletionists delete Wikipedia articles that they consider lame.
 Chuck Norris deletes deletionists whom he considers lame.

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



Deleting last 10 lines of a file

2010-03-23 Thread sheela b
Hi All,

How to delete last 10 lines of a file using Perl one liner?

I used the following one liner to delete first 10 lines of a file,

perl -i.bak -ne 'print unless 1..10' test.txt


Regards
Sheela


AW: Deleting last 10 lines of a file

2010-03-23 Thread Thomas Bätzler
sheela b sheela.b.2...@gmail.com asked:
 How to delete last 10 lines of a file using Perl one liner?
 
 I used the following one liner to delete first 10 lines of a file,
 
 perl -i.bak -ne 'print unless 1..10' test.txt

OTTOH:

perl -i.bak -ne 'BEGIN { $b[9]= } print shift @b; push @b, $_' 
test.txt

@b is the bucket chain of 10 initially empty strings. Inside the implied 
while() loop, new text is added at the end of the chain while the head element 
is pruned and then printed. The loop will terminate on eof(), leaving the last 
10 lines of text in the array.

If you're dealing with small files, another option would be to slurp in the 
file to an array, delete the last 10 items of the array and write the file back 
out to disk.

HTH,
Thomas

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




Re: Deleting last 10 lines of a file

2010-03-23 Thread Shlomi Fish
Hi Sheela,

On Tuesday 23 Mar 2010 15:06:24 sheela b wrote:
 Hi All,
 
 How to delete last 10 lines of a file using Perl one liner?
 
 I used the following one liner to delete first 10 lines of a file,
 
 perl -i.bak -ne 'print unless 1..10' test.txt
 

If it doesn't have to be a one-liner, you can do it efficiently using the 
following script:


#!/usr/bin/perl

use strict;
use warnings;

use File::ReadBackwards;
use Getopt::Long;

my $count;
GetOptions(
n=i = \$count,
);

if (!defined($count))
{
die Please specify -n for the line count.;
}

my $filename = shift;

my $bw = File::ReadBackwards-new($filename)
or die Could not backwards-open '$filename' - $!;

foreach my $idx (1 .. $count)
{
$bw-readline();
}

my $wanted_len = $bw-tell();

$bw-close();

open my $truncate_fh, +, $filename
or die Could not truncate-open '$filename' - $!;

truncate($truncate_fh, $wanted_len);

close($truncate_fh);


If you still insist on it being a one-liner, see:

http://mail.perl.org.il/pipermail/perl/2005-August/007316.html

You can do it as a one liner in a much less elegant way by using a cyclical 
queue, but I can't be bothered to implement it.

Regards,

Shlomi Fish

 
 Regards
 Sheela

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Parody on The Fountainhead - http://shlom.in/towtf

Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.

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

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