tail, start reading at EOF

R

Rebhan, Gilbert

Hi,

how to alter tail function to start at the end
of the file, not reading the lines already existing in file ? =3D

File =3D File.open(ARGV[0])=20
while File.exists?(file.path)
puts file.gets while !file.eof?
sleep(1)
end

Regards, Gilbert
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 22. Aug 2007, 18:20:12 +0900 schrieb Rebhan, Gilbert:
how to alter tail function to start at the end
of the file, not reading the lines already existing in file ? =

file.seek 0, IO::SEEK_END
File = File.open(ARGV[0])
while File.exists?(file.path)
puts file.gets while !file.eof?
sleep(1)
end

File#eof? does not work here. When the file has been
extended it still returns true even though File#gets will
return a String object.

Bertram
 
R

Rebhan, Gilbert

=20
-----Original Message-----
From: Bertram Scharpf [mailto:[email protected]]=20
Sent: Wednesday, August 22, 2007 12:14 PM
To: ruby-talk ML
Subject: Re: tail, start reading at EOF

/*
Hi,

Am Mittwoch, 22. Aug 2007, 18:20:12 +0900 schrieb Rebhan, Gilbert:
how to alter tail function to start at the end
of the file, not reading the lines already existing in file ? =3D

file.seek 0, IO::SEEK_END
*/

thanks, with your pointer and an older post by Robert Klemme
i have a working solution now =3D

def tail(file, interval=3D1)=20
raise "Illegal interval #{interval}" if interval < 0=20

File.open(file) do |io|
io.seek 0, IO::SEEK_END
loop do
while ( line =3D io.gets )
puts line
end=20
# uncomment next to watch what is happening=20
puts "-"=20
sleep interval=20
end=20
end=20
end

Anything to improve ?

Regards, Gilbert
 
R

Rob Biedenharn

-----Original Message-----
From: Bertram Scharpf [mailto:[email protected]]
Sent: Wednesday, August 22, 2007 12:14 PM
To: ruby-talk ML
Subject: Re: tail, start reading at EOF

/*
Hi,

Am Mittwoch, 22. Aug 2007, 18:20:12 +0900 schrieb Rebhan, Gilbert:
how to alter tail function to start at the end
of the file, not reading the lines already existing in file ? =

file.seek 0, IO::SEEK_END
*/

thanks, with your pointer and an older post by Robert Klemme
i have a working solution now =

def tail(file, interval=1)
raise "Illegal interval #{interval}" if interval < 0

File.open(file) do |io|
io.seek 0, IO::SEEK_END
loop do
while ( line = io.gets )
puts line
end
# uncomment next to watch what is happening
puts "-"
sleep interval
end
end
end

Anything to improve ?

Regards, Gilbert

gem install file-tail

Then look at the rdoc for it. There's an example that does an
implementation of the Unix tail command. It's a lot more capable
that what you have here, but if you wanted to start some number of
lines before the EOF, the file-tail gem is really what you want.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

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
474,264
Messages
2,571,315
Members
48,001
Latest member
Wesley9486

Latest Threads

Top