Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How can I grab the last 2 lines of a file?

by LanX (Saint)
on Jul 26, 2014 at 22:04 UTC ( [id://1095183]=note: print w/replies, xml ) Need Help??


in reply to How can I grab the last 2 lines of a file?

You need to seek on a file handle to jump to the end.

Since its byte and not line based you'll need to read a sufficiently big trailing chunk into a line array and take the last two entries. (and if the chunk wasn't big enough try a bigger one )

Search the archives, similar questions have been asked before and adjust the code to your needs.

> Ideally, I would like to store the second-to-last line to ...

Ideally someone would bring me now a banana split and a hot espresso to my sofa...

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

update

... or a strudel with vanilla ice cream... Hmm! :)

update

No espresso yet? Hence no tip! :(

But at least a link http://www.google.com/search?q=read+last+n+lines+of+a+file+perl+site:www.perlmonks.org

  • Comment on Re: How can I grab the last 2 lines of a file?

Replies are listed 'Best First'.
Re^2: How can I grab the last 2 lines of a file?
by 1s44c (Scribe) on Jul 27, 2014 at 09:47 UTC

    That's exactly the approach the unix tail command takes so it seems reasonable to assume there isn't a better way.

    strace tail -2 xterm.faq.html ... open("xterm.faq.html", O_RDONLY) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=257717, ...}) = 0 lseek(3, 0, SEEK_CUR) = 0 lseek(3, 0, SEEK_END) = 257717 lseek(3, 253952, SEEK_SET) = 253952 read(3, "href=\"manpage/resize.ps\">ps</a>\n"..., 3765) = 3765 ... write(1, "</body>\n", 8</body> ) = 8 write(1, "</html>\n", 8</html> ) = 8 read(3, "", 0) = 0 close(3) = 0 ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found