remove last 10 lines of all files in a directory

Discussion in 'Perl Misc' started by rsarpi@gmail.com, Sep 26, 2006.

  1. Guest

    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";
     
    , Sep 26, 2006
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Sep 26, 2006
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, Sep 26, 2006
    #3
  4. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 26, 2006
    #4
  5. Uri Guttman Guest

    >>>>> "JWK" == John W Krahn <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Sep 27, 2006
    #5
  6. 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
     
    Charles DeRykus, Sep 30, 2006
    #6
  7. Charles DeRykus wrote:
    > 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
     
    John W. Krahn, Sep 30, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    1
    Views:
    549
    Eliyahu Goldin
    Jun 23, 2004
  2. Replies:
    1
    Views:
    611
    Evertjan.
    Sep 30, 2005
  3. kbass
    Replies:
    2
    Views:
    1,890
    Graham Fawcett
    Nov 13, 2003
  4. Replies:
    1
    Views:
    194
    Evertjan.
    Sep 30, 2005
  5. Jack Wang
    Replies:
    5
    Views:
    108
    Mario D'Alessio
    Feb 15, 2008
Loading...

Share This Page