Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Print last line in a file

by tachyon (Chancellor)
on Aug 22, 2001 at 03:45 UTC ( [id://106825]=note: print w/replies, xml ) Need Help??


in reply to Print last line in a file

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-25 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found