我正在使用Golang Docker SDK输出容器日志。容器正在运行扫描,并输出有关扫描作业开始时间,结束时间,平均扫描持续时间的特定信息,如下所示:

    Selected XML parser javax.xml.bind.util.JAXBSource$1 does not recognize the feature http://xml.org/sax/features/validation
    Generated ./reports/CSR1000V_RTR2.json
    Generated ./reports/CSR1000V_RTR6.json
    Generated ./reports/CSR1000V_RTR3.json
Scan start time: Mon Aug 27 03:39:24 GMT 2018
Scan end time:   Mon Aug 27 03:39:40 GMT 2018
Mean target scan duration: 3906ms

我使用下面的代码将io.Reader转换为字符串:
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{
    ShowStdout: true,
    Follow:     true})
if err != nil {
    panic(err)
}
defer out.Close()
//io.Copy(os.Stdout, out)
b, err := ioutil.ReadAll(out)

fmt.Println(string(b))

如何在Golang中仅解析最后3行并仅从stdout捕获以下值:
fmt.Println("The scan has started at: " +startime)
fmt.Println("The scan has ended at: " +endtime)
fmt.Println("The scan job took xxx ms to scan each device")

最佳答案

另一种解决方案是使用bufio.Scanner API。

如果您要处理的大型输出不想将其全部存储在内存中进行处理,可能会很有趣。

扫描仪由阅读器,输入数据和拆分功能组成,以创建有意义的数据块。

使用标准的api,可以利用提供的bufio.ScanLines函数按行分割输出,然后简单的前缀等式将产生必要的信息以标识所研究的信息。

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "strings"
)

type result struct {
    start string
    end   string
    mean  string
}

func main() {
    raw := `
    Selected XML parser javax.xml.bind.util.JAXBSource$1 does not recognize the feature http://xml.org/sax/features/validation
    Generated ./reports/CSR1000V_RTR2.json
    Generated ./reports/CSR1000V_RTR6.json
    Generated ./reports/CSR1000V_RTR3.json
Scan start time: Mon Aug 27 03:39:24 GMT 2018
Scan end time:   Mon Aug 27 03:39:40 GMT 2018
Mean target scan duration: 3906ms
`
    scanner := bufio.NewScanner(strings.NewReader(raw))

    var res result
    // Create a custom split function by wrapping the existing ScanLines function.
    split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
        advance, token, err = bufio.ScanLines(data, atEOF)
        if err == nil && token != nil {
            if pattern := []byte("Scan start time:"); bytes.HasPrefix(token, pattern) {
                res.start = strings.TrimSpace(string(token[len(pattern):]))
            } else if pattern := []byte("Scan end time:"); bytes.HasPrefix(token, pattern) {
                res.end = strings.TrimSpace(string(token[len(pattern):]))
            } else if pattern := []byte("Mean target scan duration:"); bytes.HasPrefix(token, pattern) {
                res.mean = strings.TrimSpace(string(token[len(pattern):]))
            }
        }
        return
    }
    // Set the split function for the scanning operation.
    scanner.Split(split)
    // drain the source
    for scanner.Scan() {
    }

    if err := scanner.Err(); err != nil {
        fmt.Printf("Invalid input: %s", err)
    }
    fmt.Printf("%#v\n", res)
}

10-01 12:23