Reading last (or last few) lines from a very large file

4,928 views
Skip to first unread message

Bratish Goswami

unread,
Oct 4, 2010, 8:33:44 AM10/4/10
to nodejs
Hi,

How can I read last line or last few lines from a large file in
node.js?

Basically I need to have a lookup over a log file which is increasing
by size. I am trying to use watchFile to get the change and then read
the last appended line.

Any help is appreciated.

-Bratish

Oleg "Sannis" Efimov

unread,
Oct 4, 2010, 9:02:16 AM10/4/10
to nodejs
"Unix way" is to use specialized tools such as tail[1]. I think -f
option is useful fior you. You can execute tail in child process.

[1]: http://en.wikipedia.org/wiki/Tail_(Unix)

Bratish Goswami

unread,
Oct 5, 2010, 1:12:29 AM10/5/10
to nodejs
'tail -f' wont work for me. Because eventually it will get bigger and
the problem will be the same...
Is there any way to do it programmatically?

-Bratish



On Oct 4, 6:02 pm, "Oleg \"Sannis\" Efimov" <efimo...@gmail.com>
wrote:
> "Unix way" is to use specialized tools such as tail[1]. I think -f
> option is useful fior you. You can execute tail in child process.
>
> [1]:http://en.wikipedia.org/wiki/Tail_(Unix)
>

mscdex

unread,
Oct 5, 2010, 2:08:13 AM10/5/10
to nodejs
On Oct 5, 1:12 am, Bratish Goswami <bratishgosw...@gmail.com> wrote:
> 'tail -f' wont work for me. Because eventually it will get bigger and
> the problem will be the same...

I'm not sure I understand how 'tail -f' executed in a child process in
node wouldn't work for you.

Kami

unread,
Oct 5, 2010, 12:05:48 PM10/5/10
to nodejs
If you are interested in doing "tail -F" programmatically in node,
that is how I did it using watchFile -
http://github.com/cloudkick/cast/blob/master/lib/util/tail.js#L38

Marco Rogers

unread,
Oct 5, 2010, 1:54:55 PM10/5/10
to nodejs
If you're saying you don't want it to update with -f, you can just run
tail and specify the number of lines.

> tail -5 /your/file/here.txt

Vitali Lovich

unread,
Oct 5, 2010, 7:45:38 PM10/5/10
to nod...@googlegroups.com
Another way would be to seek to the end of the file, mark that offset, wait for change, seek to the end of the file, print out the contents between the two markers - that will represent the added content (not necessarily split on lines though).

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


mscdex

unread,
Oct 5, 2010, 8:13:05 PM10/5/10
to nodejs
On Oct 5, 7:45 pm, Vitali Lovich <vlov...@gmail.com> wrote:
> Another way would be to seek to the end of the file, mark that offset, wait
> for change, seek to the end of the file, print out the contents between the
> two markers - that will represent the added content (not necessarily split
> on lines though).

I was going to suggest that earlier, but I was under the impression
there was an issue with addressing very large files due to the limits
of the 'number' data type? There was a thread about it somewhere but I
wasn't sure if a fix or workaround had been made available.

Bratish Goswami

unread,
Oct 6, 2010, 1:09:12 AM10/6/10
to nodejs
Sorry to inform you all lately. I've solved this issue in a similar
line as suggested by Vitali Lovich.

Have a look in my implementation at http://gist.github.com/612849
and let me know if any optimization possible.

Thanks
Bratish
Reply all
Reply to author
Forward
0 new messages