Re: remove last 10 lines of all files in a directory



rsarpi@xxxxxxxxx wrote:
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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.