Read last line of a file

S

Shuaib Zahda

Hi all

I was trying to find a method to read only the last line of a file. I
checked the the documentation and there is no such method.
Beside reading line after line and checking the the eof method. Is there
any faster or shorter way to do that.

Regards
 
G

Gregory Seidman

Hi all

I was trying to find a method to read only the last line of a file. I
checked the the documentation and there is no such method.
Beside reading line after line and checking the the eof method. Is there
any faster or shorter way to do that.

Depending on your platform, you might find that using popen with tail -1
will be the fastest and easiest way. Failing that, consider this:

last = File.open(myfile) { |f| f.extend(Enumerable).inject { |_,ln| ln } }
--Greg
 
X

Xavier Noria

I was trying to find a method to read only the last line of a file. I
checked the the documentation and there is no such method.
Beside reading line after line and checking the the eof method. Is
there
any faster or shorter way to do that.

In Perl there's File::ReadBackwards:

http://search.cpan.org/~uri/File-ReadBackwards-1.04/ReadBackwards.pm

The idea is that you put the file object in binmode and pick a chunk
of bytes towards the end of the file using for example

file.seek(-chunk_size_in_bytes, IO::SEEK_END)

and see whether the last (or last-but-one) line separator is there. If
it is not, step a bit backwards and try again.

-- fxn
 
R

Robert Klemme

2007/11/19 said:
Depending on your platform, you might find that using popen with tail -1
will be the fastest and easiest way. Failing that, consider this:

last = File.open(myfile) { |f| f.extend(Enumerable).inject { |_,ln| ln } }

You do not need the ".extend(Enumerable)" bit as an IO does already
include Enumerable:

$ ruby -e 'p Enumerable === $stdin'
true

Cheers

robert
 
G

Gregory Seidman

You do not need the ".extend(Enumerable)" bit as an IO does already
include Enumerable:

$ ruby -e 'p Enumerable === $stdin'
true

Ah, sorry, you are correct. For some reason I was thinking of MySQL result
sets.
Cheers
robert
--Greg
 
S

Shuaib Zahda

Hi
Sorry for posting late

I was trying to figure out how to use this method but I failed. Can u
please give me an example
 
L

Lee Jarvis

Shuaib said:
Hi
Sorry for posting late

I was trying to figure out how to use this method but I failed. Can u
please give me an example

It works just like File#each, except in reverse order..

http://elif.rubyforge.org/ for documentation.

You could also just do something like this..

File.readlines(file).reverse_each {

But of course, that reads the whole file into an array first, so I would
still to using elif.

Regards,
Lee
 
J

James Edward Gray II

I was trying to figure out how to use this method but I failed. Can u
please give me an example

require "elif"

last_line = Elif.open(path) { |f| f.gets }

James Edward Gray II
 
B

Brian Adkins

Hi all

I was trying to find a method to read only the last line of a file. I
checked the the documentation and there is no such method.
Beside reading line after line and checking the the eof method. Is there
any faster or shorter way to do that.

last_line = `tail -n 1 #{file_name}`
 
S

Shuaib Zahda

James said:
require "elif"

last_line = Elif.open(path) { |f| f.gets }

James Edward Gray II

Thanks it works. But when i used require "elif". My ruby did not
recognize its path. So i copied it into my local directory and ran it
fine. Is there any path issue that i have to tackle when i download new
package from gem. I am using ubuntu 7.10

Regards
 
S

Shuaib Zahda

From: Shuaib Zahda [mailto:[email protected]]
# Thanks it works. But when i used require "elif". My ruby did not
# recognize its path. So i copied it into my local directory and ran it
# fine. Is there any path issue that i have to tackle when i
# download new package from gem. I am using ubuntu 7.10

try,
require 'rubygems'

gems require rubygems to find themselves :)
require is change thereupon..

Thanks a lot. It works well. So, I can conclude that I have to call
rubygems then I call the elif.

Regards
 
P

Peña, Botp

T24gQmVoYWxmIE9mIFNodWFpYiBaYWhkYQ0KIyBTbywgSSBjYW4gY29uY2x1ZGUgdGhhdCBJIGhh
dmUgdG8gY2FsbCBydWJ5Z2VtcyB0aGVuIEkgY2FsbCB0aGUgZWxpZi4NCg0KeWVzLCBhbmQgZm9y
IGFueSBnZW0geW91IHdhbnQgdG8gbG9hZCBmb3IgdGhhdCBtYXR0ZXIuDQoNCmJ1dCBpZiB5b3Ug
YWx3YXlzIG5lZWQgYSBnZW0sIHlvdSBjYW4gY3JlYXRlIGFuIGVudiB2YXIgUlVCWU9QVCBhbmQg
c2V0IGl0IHRvIHJ1YnlnZW1zLCBsaWtlDQogIA0KICAgZXhwb3J0IFJVQllPUFQ9cnVieWdlbXMN
Cg0Kbm90ZSB0aGF0IGkgc2VsZG9tIGRvIHRoaXMgc2luY2UgcnVieWdlbXMgbG9hZHMgYSBsb3Qg
b2YgdGhpbmdzIChvYnNlcnZlIHlvdXIgJCIpLiBpIHdpc2ggcnVieWdlbXMgaGFzIHNvbWV0aGlu
ZyBsaWtlLCANCg0KICAgcmVxdWlyZSAicnVieWdlbXMtYmFzZSINCg0Kd2MganVzdCBpbnN0YWxs
cyB0aGUgYmFzaWMgZ2VtIG5lZWRlZCB0byBmaW5kIHRoZSBuZWVkZWQgZ2VtIHBhdGhzLi4NCg0K
a2luZCByZWdhcmRzIC1ib3RwDQo=
 
P

Peña, Botp

From: James Edward Gray II [mailto:[email protected]]=20
# require "elif"
# last_line =3D Elif.open(path) { |f| f.gets }

cool.

wc reminds me, i'm looking for a gem that reverses the behaviour of =
string/collection/io/ traversals, like

#file reverse
require "in_reverse"
last_line =3D File.open(path).in_reverse { |f| f.gets }

#array reverse
%w(this is a test).each.in_reverse {|x| p x}
test
a
is
this

#string reverse
"this is a test".each_char.in_reverse {|c| p c; break if c=3D=3D"a"}
t
s
e
t

a

#
kind regards -botp
 
J

James Edward Gray II

wc reminds me, i'm looking for a gem that reverses the behaviour of =20=
string/collection/io/ traversals, like
#array reverse
%w(this is a test).each.in_reverse {|x| p x}
test
a
is
this

This can be hard to support for all iterators, but Array does have =20
reverse_each():
%w[this is a test].reverse_each { |word| puts word }
test
a
is
this
=3D> ["this", "is", "a", "test"]

James Edward Gray II=
 

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,787
Messages
2,569,629
Members
45,332
Latest member
LeesaButts

Latest Threads

Top