For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2006 > Remove last 10 lines of all files in a directory









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Remove last 10 lines of all files in a directory
rsarpi@gmail.com

2006-09-26, 6:57 pm

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

usenet@DavidFilmer.com

2006-09-26, 6:57 pm

rsarpi@gmail.com wrote:

> Objective: remove last 10 lines of *all* files in a directory


#!/usr/bin/perl
use strict; use warnings;
use Tie::File;

foreach my $filename(glob('/path/to/my/*.*')) {
tie my @file, 'Tie::File', $filename or die "oops - $!";
@file = @file[ 0 .. ($#file - 10) ];
untie @file;
}

__END__

DJ Stunks

2006-09-26, 6:57 pm


usenet@DavidFilmer.com wrote:
> @file = @file[ 0 .. ($#file - 10) ];


$#file -= 10;

:)

-jp

usenet@DavidFilmer.com

2006-09-26, 6:57 pm

DJ Stunks wrote:
> $#file -= 10;


Hey, no kidding? I never realized $#foo could be assigned to... that's
a neat trick.

--
David Filmer (http://DavidFilmer.com)

DJ Stunks

2006-09-26, 6:57 pm

usenet@DavidFilmer.com wrote:
> DJ Stunks wrote:
>
> Hey, no kidding? I never realized $#foo could be assigned to... that's
> a neat trick.


yeah, it is.
[color=darkred]

pd> The length of an array is a scalar value. You may find
pd> the length of array @days by evaluating $#days, as in
pd> csh. However, this isn't the length of the array; it's
pd> the subscript of the last element, which is a different
pd> value since there is ordinarily a 0th element. Assigning
pd> to $#days actually changes the length of the array.
pd> Shortening an array this way destroys intervening values.
pd> Lengthening an array that was previously shortened does
pd> not recover values that were in those elements. (It used
pd> to do so in Perl 4, but we had to break this to make
pd> sure destructors were called when expected.)

-jp

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com