Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Read last lines from a big file with Go every 10 secs

Tags:

how can I read the last two lines from a big log file without load it into memory completely?

I need read it every 10 secs(On a Win machine)...and I'm stuck trying to read the last lines..

package main  import (     "fmt"     "time"     "os" )  const MYFILE = "logfile.log"  func main() {     c := time.Tick(10 * time.Second)     for now := range c {         readFile(MYFILE)     } }  func readFile(fname string){     file, err:=os.Open(fname)     if err!=nil{         panic(err)     }     buf:=make([]byte, 32)     c, err:=file.ReadAt(32, ????)     fmt.Printf("%s\n", c)   } 

The log file is something like:

07/25/2013 11:55:42.400, 0.559 07/25/2013 11:55:52.200, 0.477 07/25/2013 11:56:02.000, 0.463 07/25/2013 11:56:11.800, 0.454 07/25/2013 11:56:21.600, 0.424 07/25/2013 11:56:31.400, 0.382 07/25/2013 11:56:41.200, 0.353 07/25/2013 11:56:51.000, 0.384 07/25/2013 11:57:00.800, 0.393 07/25/2013 11:57:10.600, 0.456 

Thanks!

like image 811
Goku Avatar asked Jul 25 '13 16:07

Goku


People also ask

How can you read last 10 lines of a big file?

To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.

How do I read a large file in GoLang?

the easiest way to read a file (at least for me) is using the scanner from the bufio package in the standard library. First, we need to create an instance with the NewScanner function which is a very familiar way for constructing the structs in golang.

How do I read the last part of a file in Python?

To read the last line of a file in Python without storing the entire file in memory: Loop through each line in the file. Skip each line until you reach the last line. Save the last line in memory.

How do I read a chunk in GoLang?

Read the entire file in GoLang In Go, Reading an entire file content/text is to use ReadFile() function from the ioutil/os package. This function reads the entire content of the file into a byte slice. The ioutil package should not be used in reading a large file thus the function is quite sufficient for small files.


1 Answers

You can use file.Seek() or file.ReadAt() to almost the end and then Reading forward. You can only estimate where to start seeking unless you can know that 2 lines = x bytes.

You can get the File length by using the os.Stat(name)

Here is an example based on ReadAt, Stat, and your sample log file:

package main  import (     "fmt"     "os"     "time" )  const MYFILE = "logfile.log"  func main() {     c := time.Tick(10 * time.Second)     for _ = range c {         readFile(MYFILE)     } }  func readFile(fname string) {     file, err := os.Open(fname)     if err != nil {         panic(err)     }     defer file.Close()      buf := make([]byte, 62)     stat, err := os.Stat(fname)     start := stat.Size() - 62     _, err = file.ReadAt(buf, start)     if err == nil {         fmt.Printf("%s\n", buf)     }  } 
like image 158
Joshua Avatar answered Sep 24 '22 21:09

Joshua