Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > remove last 10 lines of all files in a directory

Reply
Thread Tools

remove last 10 lines of all files in a directory

 
 
rsarpi@gmail.com
Guest
Posts: n/a
 
      09-26-2006
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";

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-26-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-26-2006
(E-Mail Removed) 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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-26-2006
(E-Mail Removed) <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      09-27-2006
>>>>> "JWK" == John W Krahn <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Charles DeRykus
Guest
Posts: n/a
 
      09-30-2006
(E-Mail Removed) 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

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      09-30-2006
Charles DeRykus wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any Idea why this code doesn't remove all the blank lines? Jack Wang Perl Misc 5 02-15-2008 03:54 PM
show all files in the directory, but how to show directory control?? strutsng@gmail.com HTML 1 09-30-2005 07:32 AM
show all files in the directory, but how to show directory control?? strutsng@gmail.com ASP General 1 09-30-2005 07:32 AM
Re: Why I can not remove all dialers after "remove"-I have two anti-dialers programs Joseph Ladovic Computer Security 3 05-26-2005 03:00 AM
list all css files in a directory and sub directory TJS ASP .Net 1 06-23-2004 10:49 AM



Advertisments