From: Ernest Ellingson Date: 2001-03-26T00:56:07+09:00 Subject: [ruby-talk:13198] Re: Reading a file backwards -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 - -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 At 16:50 3/25/2001 +0900, you wrote: - - -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sun, 25 Mar 2001 14:10, I think Daniel Berger wrote: > Hi all, > > Old Perl programmer, new to Ruby. Is there a method/module out there for > reading a file backwards? This would be useful for logfiles, for example. > > Thanks in advance. > > Dan Hi Dan, perhaps something like this is what you want? synack@eugene:~/tmp/fox-0.99.166/textedit$ cat > testFile line 1 line 2 line 3 synack@eugene:~/tmp/fox-0.99.166/textedit$ irb irb(main):001:0> f = File.new("testFile") # irb(main):002:0> f.readlines.reverse.each { |line| p line } "line 3\n" "line 2\n" "line 1\n" ["line 3\n", "line 2\n", "line 1\n"] Here's a little ruby script I use for watching a log file if(!ARGV[0] || !ARGV[1]) puts "usage ruby tailReverse.rb Filename bytesToRead [interval in secs]" exit end x=ARGV[0] y=ARGV[1] z=ARGV[2] tint=60 if !z tint=60 else tint=z.to_i end while tint > 0 do f=File.new(x) s=f.stat if s.size <= y.to_i f.each {|line| puts "#{line}"} else f.seek(-y.to_i, IO::SEEK_END) f.readlines.reverse.each {|line| puts "#{line}"} end f.close puts "--------------------------------------------------" sleep(tint) end -----BEGIN PGP SIGNATURE----- Version: PGP for Personal Privacy 5.5.2 iQA/AwUBOr4PKNjbcG3JH9DeEQIGsACgpdYtW2+UJWVbroIezgX1O63A9mAAoJYs /ZXVB3id/VtJz0ZlWyQfwveK =dQJr -----END PGP SIGNATURE-----