Read from middle/near end of files?

Hi,

Does anyone know if it is possible to read say the last 10 line of a
file? readlines would work if memory is unlimited, but that isn’t the
case, especially if the file is a 2GB log file. Or is this not possible
at all?

Thanks.

Does anyone know if it is possible to read say the last 10 line of a
file? readlines would work if memory is unlimited, but that isn’t the
case, especially if the file is a 2GB log file. Or is this not possible
at all?

Never used it, but a google search for ‘ruby file tail’ turns up this:

http://raa.ruby-lang.org/project/file-tail/

You could also get the size of the file, open it, and then seek to
somewhere near the end and just start reading at that point till you
reach
the end…

-philip

On Feb 16, 2007, at 11:57 AM, Philip H. wrote:

You could also get the size of the file, open it, and then seek to
somewhere near the end and just start reading at that point till
you reach
the end…

-philip

I have used file-tail and taken a look at the code. It seems to be
well written and covers possible gotchas quite well. For example, to
get the revision number out of the revisions.log file that capistrano
adds a line to on each deploy:

     rev = File.open(name) do |f|
       f.extend(File::Tail)
       f.rewind(1).gets.match(/(\d+) \d{14}$/)[1]
     end

You gotta like that!

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Thanks a lot. file-tail is exactly what I am looking for.