golang模仿tail命令,显示文件末尾指定行数的文件内容

2023-12-19 21:33:47

import (
	"bufio"
	"bytes"
	"io"
	"log"
	"os"
	"strings"
)

func main() {
	println(Tail("/Users/liyinlong/a.log", 3))
}

func Tail(path string, num int) string {
	lines := int64(num)
	file, err := os.Open(path)
	if err != nil {
		log.Println("读取文件内容出错了 %v", err)
		return ""
	}
	fileInfo, _ := file.Stat()
	buf := bufio.NewReader(file)
	offset := fileInfo.Size() % 8192
	data := make([]byte, 8192) // 一行的数据
	totalByte := make([][][]byte, 0)
	readLines := int64(0)
	for i := int64(0); i <= fileInfo.Size()/8192; i++ {
		readByte := make([][]byte, 0) // 读取一页的数据
		file.Seek(fileInfo.Size()-offset-8192*i, io.SeekStart)
		data = make([]byte, 8192)
		n, err := buf.Read(data)
		if err == io.EOF {
			if strings.TrimSpace(string(bytes.Trim(data, "\x00"))) != "" {
				readLines++
				readByte = append(readByte, data)
				totalByte = append(totalByte, readByte)
			}
			if readLines > lines {
				break
			}
			continue
		}
		if err != nil {
			log.Println("Read file error:", err)
			return ""
		}
		strs := strings.Split(string(data[:n]), "\n")
		if len(strs) == 1 {
			b := bytes.Trim([]byte(strs[0]), "\x00")
			if len(b) == 0 {
				continue
			}
		}
		if (readLines + int64(len(strs))) > lines {
			strs = strs[int64(len(strs))-lines+readLines:]
		}
		for j := 0; j < len(strs); j++ {
			readByte = append(readByte, bytes.Trim([]byte(strs[j]+"\n"), "\x00"))
		}
		readByte[len(readByte)-1] = bytes.TrimSuffix(readByte[len(readByte)-1], []byte("\n"))
		totalByte = append(totalByte, readByte)
		readLines += int64(len(strs))

		if readLines >= lines {
			break
		}
	}
	totalByte = ReverseByteArray(totalByte)
	return ByteArrayToString(totalByte)
}

func ReverseByteArray(s [][][]byte) [][][]byte {
	for from, to := 0, len(s)-1; from < to; from, to = from+1, to-1 {
		s[from], s[to] = s[to], s[from]
	}
	return s
}

func ByteArrayToString(buf [][][]byte) string {
	str := make([]string, 0)
	for _, v := range buf {
		for _, vv := range v {
			str = append(str, string(vv))
		}
	}
	return strings.Join(str, "")
}

文章来源:https://blog.csdn.net/LONG_Yi_1994/article/details/135089944
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。