Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Print last line in a file

by dreman (Acolyte)
on Aug 22, 2001 at 03:03 UTC ( #106808=perlquestion: print w/replies, xml ) Need Help??

dreman has asked for the wisdom of the Perl Monks concerning the following question:

open(FH, "< $err_out") or die "can't open LOG FILE $!"; while (<FH>) { print "$_" } close FH;
How do you print the last line in a file without printing all the lines in the file. Thanks.

Edit kudra, 2001-08-22 Added code tags

Replies are listed 'Best First'.
Re: Print last line in a file
by runrig (Abbot) on Aug 22, 2001 at 03:08 UTC
    If its a large file, I'd use File::ReadBackwards, otherwise just save the lines as you read them in a temp variable, then print that variable after the while loop:
    my $tmp; $tmp = $_ while <FH>; print $tmp;
      If you want to get a bit crazy, you can do something like the following:

      undef $/;
      $_ = <>;
      print $1 if /([^\r\n]+\r?\n?)$/;
      

      Of course, that reads the whole file into memory, which is generally a bad idea if you don't need to do it... but hey, there is always more than one way to do it.

      Maybe somebody can provide an example of tying a variable to the file and then using the regex? Personally, Runrig's example is probably the best way to do it.

      Zucan

Re: Print last line in a file
by lemming (Priest) on Aug 22, 2001 at 04:02 UTC

    You can also jump down to the end of the file:

    open(FILE, "< $err_out") or die $!; seek(FILE, -200, 2); my @lastones = <FILE>; print $lastones[-1];
    That way, even if it's a huge file you only grab the last 200 bytes. Though, this will only get part of the last line if the line is longer than 200 bytes. One could check for a return and if not present grab further back.

Re: Print last line in a file
by perigeeV (Hermit) on Aug 22, 2001 at 05:39 UTC
    I've always done it as one-liner:

    perl -ne '$last=$_; END{print $last}'

    Similar in spirit to tachyon's reply.

Re: Print last line in a file
by tachyon (Chancellor) on Aug 22, 2001 at 03:45 UTC

    Using what you have already a simple modification does it - we just loop through the file setting each line to $last. At the end of the while loop $last will actually be the last line! This is momory efficient:

    open(FH, "< $err_out") or die "can't open LOG FILE $!"; while (<FH>) { + $last = $_; } close FH; print "Last line was: ", $last; Alternatively you could just do this ad grab the whole file in an arra +y: open FH, $file or die $!; @file = <FH>; close FH; print "Last 4 lines, backwards: ", @file[-1,-2,-3,-4];

    PS if you enclose your code in <code> </code> tags it will retain its formatting.

    Hope this helps. cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Print last line in a file
by Beatnik (Parson) on Aug 22, 2001 at 11:40 UTC
    Without slurping the entire file in an array, without the shell calls and the modules, I'd stick to
    open(FILE,"< filename") || die $!; while($line=<FILE>) {} close(FILE); print $line;

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Print last line in a file
by nehlwyn (Acolyte) on Aug 22, 2001 at 05:48 UTC

    Greetings fellow monk ,

    You've had several good answers so i had this alternative just for fun and to point at one of the Perl mottos : TIMTOWTDI !!!

    Using the same idea to quickly read all the lines but with using the special array @_

    open (FILE,"your file"); @_=<FILE>; close(FILE); print pop;

    There you go ...

    the little one

      Your open should have an error check as described in perlstyle.

      This is usually done explicitly, but the various shortcuts do so as well. For instance try this:

      @ARGV="your_file"; print((<>)[-1]);
      BTW I really did need both pairs of parens.

        Greetings fellow monk,

        You're dammmn right , sir ... 'was a bit hasty of me ... But fortun'ly , 'had a fellow monk to DWIM(eant) ...;-)

        the little one.
Re: Print last line in a file
by cLive ;-) (Prior) on Aug 22, 2001 at 07:47 UTC
    Of course, if you're on *nix, you can do:
    print `tail -n1 filename`; # (en-one)

    cLive ;-)

Re: Print last line in a file
by MZSanford (Curate) on Aug 22, 2001 at 18:53 UTC
    And, for HUGE files, i have been know to use something like :
    open(F,"< $filename"); seek(F,-2,2); while ($c ne $/) { read(F,$c,1);seek(F,-2,1); $last = "$c"."$last"; } print "Last : $last";

    can't sleep clowns will eat me
    -- MZSanford

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://106808]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-05-26 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (93 votes). Check out past polls.

    Notices?