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

**URL:** <https://discourse.julialang.org/t/how-to-read-only-the-last-line-of-a-file-txt/68005>\
**Category:** General Usage\
**Tags:** question, io\
**Created:** [September 10, 2021, 8:07pm UTC](https://discourse.julialang.org/t/how-to-read-only-the-last-line-of-a-file-txt/68005 "2021-09-10T20:07:06Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [September 12, 2021, 10:44am UTC](https://discourse.julialang.org/t/how-to-read-only-the-last-line-of-a-file-txt/68005/21 "2021-09-12T10:44:47Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 12, 2021, 11:16am UTC](https://discourse.julialang.org/t/how-to-read-only-the-last-line-of-a-file-txt/68005/22 "2021-09-12T11:16:11Z")

</div>

> [@pfitzseb](#):
>
> Sure, but it’d probably be reading in blocks from the end of the file, not single characters.

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

> [@Sukera](#):
>
> It could be improved by reading a cacheline worth of bytes starting from the end at a time, checking if any are a `'\n'` and then doing the `read(io, String)` from that index.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 12, 2021, 12:58pm UTC](https://discourse.julialang.org/t/how-to-read-only-the-last-line-of-a-file-txt/68005/23 "2021-09-12T12:58:42Z")

</div>

> [@Sukera](#):
>
> Yeah, `eachline` does algorithmically worse with more rows.

`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](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration), yielding an optimized `last` method, for streams that support `seek`.

(For reference, here is the BSD-licensed [`tail` implementation](https://github.com/landley/toybox/blob/master/toys/posix/tail.c) from [Toybox](https://en.wikipedia.org/wiki/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.)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 12, 2021, 1:00pm UTC](https://discourse.julialang.org/t/how-to-read-only-the-last-line-of-a-file-txt/68005/24 "2021-09-12T13:00:21Z")

</div>

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 🙂

One would have to implement [`iterate(t::Iterators.Reverse{EachLine})`](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.reverse) to do this, if I understood you correctly.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 12, 2021, 6:48pm UTC](https://discourse.julialang.org/t/how-to-read-only-the-last-line-of-a-file-txt/68005/25 "2021-09-12T18:48:45Z")

</div>

> [@Sukera](#):
>
> Would be a good productive PR to come out of this, if anyone is willing to take the ideas from here to an implementation

I created a PR: [https://github.com/JuliaLang/julia/pull/42225](https://github.com/JuliaLang/julia/pull/42225)

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

[Previous page](https://discourse.julialang.org/t/how-to-read-only-the-last-line-of-a-file-txt/68005.md?page=1)
