remove last 10 lines of all files in a directory

R

rsarpi

using perl 5.8.8

Objective: remove last 10 lines of *all* files in a directory with no
branches.

Like...xfiles directory has the files xfile1...xfile10. No
subdirectories. I want to remove the last 10 lines of xfile1...xfile10.

How do I do that?

I've tried to put all files in an array but I can't open all files at
one to write to them. And for some reason globbing doesn't do the
trick...like glob ("*.*");

I came up with something to delete the last 10 lines of ONE file only.
(A portion of this code is mine the other is from the perl cookbook)
----------
#!/usr/bin/perl

use warnings;
use strict;


my $path = "C:/path/to/file.txt";

my $addr = "";

#create a sentinel value
my $counter = 1;

#kicks out after it reaches line 10. We just want to delete 10 lines
while ( $counter <= 10 ) {
open( FH, "+< $path" ) or die $!;
while (<FH>) {
$addr = tell(FH) unless eof(FH);
}
truncate( FH, $addr ) or die $!;

#update sentinel value
$counter++;
}
print "all done\n";
 
J

John W. Krahn

using perl 5.8.8

Objective: remove last 10 lines of *all* files in a directory with no
branches.

Like...xfiles directory has the files xfile1...xfile10. No
subdirectories. I want to remove the last 10 lines of xfile1...xfile10.

How do I do that?

I've tried to put all files in an array but I can't open all files at
one to write to them. And for some reason globbing doesn't do the
trick...like glob ("*.*");

I came up with something to delete the last 10 lines of ONE file only.
(A portion of this code is mine the other is from the perl cookbook)
----------
#!/usr/bin/perl

use warnings;
use strict;


my $path = "C:/path/to/file.txt";

my $addr = "";

#create a sentinel value
my $counter = 1;

#kicks out after it reaches line 10. We just want to delete 10 lines
while ( $counter <= 10 ) {
open( FH, "+< $path" ) or die $!;
while (<FH>) {
$addr = tell(FH) unless eof(FH);
}
truncate( FH, $addr ) or die $!;

#update sentinel value
$counter++;
}
print "all done\n";

I don't have Windows to test this on but it works on Linux:

#!/usr/bin/perl
use warnings;
use strict;


@ARGV = <C:/path/to/*>;

my @addrs;

while ( <> ) {
push @addrs, tell ARGV;
if ( eof ) {
my $file = $ARGV;
close ARGV;
truncate $file, ( splice @addrs )[ -11 ];
}
}




John
 
T

Tad McClellan

Objective: remove last 10 lines of *all* files in a directory with no
branches.

How do I do that?


my @all_files = grep { -f } glob '* .*'; # untested

I came up with something to delete the last 10 lines of ONE file only.


Then put that in a subroutine and call the subroutine a bunch of times:

foreach my $filename ( @all_files ) {
delete_last_10_lines( $filename );
}
 
U

Uri Guttman

JWK> @ARGV = <C:/path/to/*>;

JWK> my @addrs;

JWK> while ( <> ) {
JWK> push @addrs, tell ARGV;
JWK> if ( eof ) {
JWK> my $file = $ARGV;
JWK> close ARGV;
JWK> truncate $file, ( splice @addrs )[ -11 ];
JWK> }
JWK> }

that leads me to say to use File::ReadBackwards to get the location of
the 10th to last line and then truncate. <untested and pseudoish>

use File::ReadBackwards ;

my $fh = File::ReadBackwards->new( $file ) ;

$fh->readline() for 1 .. 10 ;

truncate $file, $fh->tell() ;

uri
 
C

Charles DeRykus

using perl 5.8.8

Objective: remove last 10 lines of *all* files in a directory with no
branches.

Like...xfiles directory has the files xfile1...xfile10. No
subdirectories. I want to remove the last 10 lines of xfile1...xfile10.

How do I do that?

I've tried to put all files in an array but I can't open all files at
one to write to them. And for some reason globbing doesn't do the
trick...like glob ("*.*");

I came up with something to delete the last 10 lines of ONE file only.
(A portion of this code is mine the other is from the perl cookbook)
----------
#!/usr/bin/perl

use warnings;
use strict;


my $path = "C:/path/to/file.txt";

my $addr = "";

#create a sentinel value
my $counter = 1;

#kicks out after it reaches line 10. We just want to delete 10 lines
while ( $counter <= 10 ) {
open( FH, "+< $path" ) or die $!;
while (<FH>) {
$addr = tell(FH) unless eof(FH);
}
truncate( FH, $addr ) or die $!;

#update sentinel value
$counter++;
}
print "all done\n";

if you have "find" on your Win32 box:

find ... | perl -MTie::File -nle "chomp;tie @a,'Tie::File',$_ or die
$!;splice @a,-10"
 
J

John W. Krahn

Charles said:
if you have "find" on your Win32 box:

find ... | perl -MTie::File -nle "chomp;tie @a,'Tie::File',$_ or die
$!;splice @a,-10"

You are chomping $_ twice. Either remove the -l switch or 'chomp;'.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top