Reading last N lines from large file

C

Chris Nestrud

I need to read a number of lines from the end of a large file. For
example, the file may have 460484 lines and I need the last 500.

The "tail -500 file" command from the shell works, but I'd like a solution
that doesn't involve calling an external program.

Reading the file and keeping an array with the last 500 lines seen
works, but takes a very long time on large files.

I've looked at the seek function, but other than guessing at how far
from EOF to seek backward, I can't figure out how to make sure that I'm
left with enough data to account for the needed number of lines.

Any suggestions or other approaches would be much appreciated.
 
P

Peter Corlett

Chris Nestrud said:
I've looked at the seek function, but other than guessing at how far
from EOF to seek backward, I can't figure out how to make sure that
I'm left with enough data to account for the needed number of lines.

I suspect tail merely guesses a seek position, then reads to find out
if it was right, retrying until it gets enough data.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top