Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   remove last 10 lines of all files in a directory (http://www.velocityreviews.com/forums/t900069-remove-last-10-lines-of-all-files-in-a-directory.html)

rsarpi@gmail.com 09-26-2006 07:39 PM

remove last 10 lines of all files in a directory
 
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";


Gunnar Hjalmarsson 09-26-2006 10:49 PM

Re: remove last 10 lines of all files in a directory
 
rsarpi@gmail.com wrote:
> Objective: remove last 10 lines of *all* files in a directory with no
> branches.


perldoc -f readdir
perldoc -q "delete a line"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

John W. Krahn 09-26-2006 10:52 PM

Re: remove last 10 lines of all files in a directory
 
rsarpi@gmail.com 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

Tad McClellan 09-26-2006 10:56 PM

Re: remove last 10 lines of all files in a directory
 
rsarpi@gmail.com <rsarpi@gmail.com> wrote:


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


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Uri Guttman 09-27-2006 04:10 AM

Re: remove last 10 lines of all files in a directory
 
>>>>> "JWK" == John W Krahn <someone@example.com> writes:

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

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Charles DeRykus 09-30-2006 06:08 PM

Re: remove last 10 lines of all files in a directory
 
rsarpi@gmail.com 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";
>


if you have "find" on your Win32 box:

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


--
Charles DeRykus


John W. Krahn 09-30-2006 08:26 PM

Re: remove last 10 lines of all files in a directory
 
Charles DeRykus wrote:
> rsarpi@gmail.com wrote:
>>
>> Objective: remove last 10 lines of *all* files in a directory with no
>> branches.

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


All times are GMT. The time now is 07:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.