Read file from end

4,535 views
Skip to first unread message

Archos

unread,
Jul 30, 2012, 3:19:52 AM7/30/12
to golan...@googlegroups.com
Which would be the best way to read a file from the end?
The first solution that goes to mi head is get buffers of i.e. 10 lines starting by the end; so get 10 last lines to read from line -10 (until line -1), else get from -20 until -10, and so on.

hash

unread,
Jul 30, 2012, 4:08:27 AM7/30/12
to golan...@googlegroups.com
On Monday, 30 July 2012 09:19:52 UTC+2, Archos wrote:
Which would be the best way to read a file from the end?
The first solution that goes to mi head is get buffers of i.e. 10 lines starting by the end; so get 10 last lines to read from line -10 (until line -1), else get from -20 until -10, and so on.
    b, err := ioutil.ReadFile(filename)
    if err != nil { /*handle*/ }
    for l := len(b); 0 < l; l-- {
        doSomethingWith(b[l])
    }
this?

Archos

unread,
Jul 30, 2012, 4:20:22 AM7/30/12
to golan...@googlegroups.com
ReadFile reads all file in memory, and it goes back step to step in bytes.
I'll have to do a custom function to imitate ReadLine() but to back.

DisposaBoy

unread,
Jul 30, 2012, 4:38:17 AM7/30/12
to golan...@googlegroups.com


On Monday, July 30, 2012 8:19:52 AM UTC+1, Archos wrote:
Which would be the best way to read a file from the end?
The first solution that goes to mi head is get buffers of i.e. 10 lines starting by the end; so get 10 last lines to read from line -10 (until line -1), else get from -20 until -10, and so on.


in the case that you want to read N bytes from the end of a file and the file size is S: seek to S-N and read N

 

Benny Siegert

unread,
Jul 30, 2012, 9:04:31 AM7/30/12
to golang-nuts
On Mon, Jul 30, 2012 at 8:38 AM, DisposaBoy <dispo...@dby.me> wrote:
> in the case that you want to read N bytes from the end of a file and the
> file size is S: seek to S-N and read N

Or, even better, use the ReadAt method.

--Benny.


--
The first essential in chemistry is that you should perform practical
work and conduct experiments, for he who performs not practical work
nor makes experiments will never attain the least degree of mastery.
-- Abu Musa Jabir ibn Hayyan (721-815)

Stephen Day

unread,
Jul 30, 2012, 1:30:40 PM7/30/12
to golan...@googlegroups.com
Use the whence parameter of seek to seek to the end of the file, then search for newline characters going backwards. Once you've found the number of lines you want, seek to the beginning of the last line found (the first of your output) and wrap the reader in a bufio.Reader. The bufio.Reader will then produce lines as desired, from the end of the file.

http://golang.org/pkg/os/#File.Seek

It's also recommend that you check out the source of tail to understand how to correctly implement this functionality.

npala...@gmail.com

unread,
Jul 25, 2013, 10:32:04 AM7/25/13
to golan...@googlegroups.com
Hi Archos, what was your final solution for it?

I'm trying to solve the same thing with big log files.

Thanks in advance.
Christian.

coreym...@gmail.com

unread,
Dec 4, 2015, 1:21:36 AM12/4/15
to golang-nuts
I know this thread is old but I found it trying to find a solution to the "read from end of file" problem.  AKA i wanted a way to  "tail" in go.  Well using Stephen's basic idea (7/30/12), I built a "tailing" function in go.  Figured I would post this here in case some else comes upon this thread randomly trying to solve this same problem.

My code can be found here: https://github.com/coreymgilmore/gotail

It works by reading backwards from then end of the file.  At every "offset" (character) backward, it checks if the character is a newline (\n). When the loops finds enough newlines (-n=10, therefore 10 newline chars), it spits back all the text from this offset to the end of the file.  Does it have 100% of the functionality of "tail": Nope. Does it work: Yes.

C Banning

unread,
Dec 4, 2015, 5:59:28 AM12/4/15
to golang-nuts, coreym...@gmail.com

Manlio Perillo

unread,
Dec 4, 2015, 9:27:56 AM12/4/15
to golang-nuts, coreym...@gmail.com
Il giorno venerdì 4 dicembre 2015 07:21:36 UTC+1, coreym...@gmail.com ha scritto:
I know this thread is old but I found it trying to find a solution to the "read from end of file" problem.  AKA i wanted a way to  "tail" in go.  Well using Stephen's basic idea (7/30/12), I built a "tailing" function in go.  Figured I would post this here in case some else comes upon this thread randomly trying to solve this same problem.


Here is a POSIX compatible (?) tail implementation:

Another, less readable, implementation is from busybox:

> [...]

Regards  Manlio 

Chris Hines

unread,
Dec 4, 2015, 1:11:50 PM12/4/15
to golang-nuts, coreym...@gmail.com
There are a few packages out there that provide file tailing features. Maybe one of them does what you need.

http://go-search.org/search?q=tail

Chris
Reply all
Reply to author
Forward
0 new messages