如何从每个10秒的Go文件中读取最后一行 [英] How to Read last lines from a big file with Go every 10 secs
本文介绍了如何从每个10秒的Go文件中读取最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从大日志文件中读取最后两行而不将其完全加载到内存中?
how can I read the last two lines from a big log file without load it into memory completely?
我需要每10秒读一遍(在Win机器上)...我被卡住,试图读最后一行。
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)
}
日志文件类似于:
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!
推荐答案
您可以使用 file.Seek()或 file.ReadAt()几乎结束然后向前读取。你只能估计从哪里开始寻找,除非你知道2行= x字节。
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.
你可以通过使用 os.Stat(name)
以下是一个基于ReadAt,Stat和您的样本日志文件:
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)
}
}
这篇关于如何从每个10秒的Go文件中读取最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文