Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading the last n lines of a file in Ruby?

Tags:

file-io

ruby

I need to read the last 25 lines from a file (for displaying the most recent log entries). Is there anyway in Ruby to start at the end of a file and read it backwards?

like image 906
Josh Moore Avatar asked Apr 16 '09 02:04

Josh Moore


1 Answers

If on a *nix system with tail, you can cheat like this:

last_25_lines = `tail -n 25 whatever.txt` 
like image 102
rfunduk Avatar answered Oct 15 '22 09:10

rfunduk