How to read only the last line of a file (.txt)?

On top of which, tail would fundamentally be doing something similar, so I’d guess at best you’d still have to pay that overhead.

Sure, but it’d probably be reading in blocks from the end of the file, not single characters. Wouldn’t be particularly hard to do that directly in Julia though.

1 Like

Yep - I assumed we were already talking about a hypothetical “ideal” implementation: :slight_smile:

2 Likes

eachline(io) just creates an EachLine iterator object, so in principle it should be possible to implement alternative algorithms on top of this. For example, one could support the reverse-order iteration protocol, yielding an optimized last method, for streams that support seek.

(For reference, here is the BSD-licensed tail implementation from Toybox. You can see that it reads from the end in buffered chunks, but for stream types that don’t support seek it has a fallback path that reads through the whole file while saving a trailing buffer.)

4 Likes

Yeah - last(eachline(file)) currently errors anyway. Would be a good productive PR to come out of this, if anyone is willing to take the ideas from here to an implementation :slight_smile:

One would have to implement iterate(t::Iterators.Reverse{EachLine}) to do this, if I understood you correctly.

1 Like

I created a PR: https://github.com/JuliaLang/julia/pull/42225

Update: merged in Julia 1.8, so you can now simply do last(eachline(file)).

8 Likes