登录
首页 >  Golang >  Go问答

最有效的将shell输出绑定到Go结构的方法

来源:stackoverflow

时间:2024-02-23 16:03:24 476浏览 收藏

今天golang学习网给大家带来了《最有效的将shell输出绑定到Go结构的方法》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

我有一些芯片在某些硬件上运行,我想将 shell 命令的输出绑定到一个结构体以进行报告/登录。

num item1: 2
index load model_load inst mem  share_mem p2p_mem device         namespace
1     2    3          4    50    600         700       1     1
1a     2b    3c          4c    5d    6e         7f       2     2
num item2: 2
index load model_load inst mem  share_mem p2p_mem device         namespace
2a     2b    2c          3    0    0         0       1     1
1     0    0          0    0    0         0       2     2
**************************************************

尝试

cat out.txt | grep -i "num $1" -a 3 | grep -i nvme | tr -s ' ' | cut -d' ' -f1-7

这实际上还不错,我可以传入像 decodersencoders 这样的参数并获取每个芯片的负载指标。然而,我现在很好奇将其绑定到 go 中的结构的最佳方法。

目前,我能做的就是编写一个自定义反序列化器,例如:

func main() {
    out, err := exec.Command("/bin/sh", "metrics.sh", "encoders").Output()
    if err != nil {
        fmt.Println(err)
        log.Fatal(err)
    }

    fmt.Println(string(out))
}

但我觉得必须有更好的方法,比如输出为 json 并绑定到结构或其他东西。


正确答案


我会将您的输入文件转换为 csv,因为它适合原始表格数据,而且 go 语言的标准库中有一个 csv 编码器/解码器:

awk -v ofs=',' '
    $1 == "num" {
        count = $3
        type = $2

        getline
        if ( !header++ ) {
            $(nf+1) = "id"
            print
        }
        for ( id = 1; id <= count; id++ ) {
            getline
            $(nf+1) = type id
            print
        }
    }
' file.txt

警告:代码不会对字段进行 csv 转义

index,load,model_load,inst,mem,share_mem,p2p_mem,device,namespace,id
1,2,3,4,50,600,700,/dev/nvme0,/dev/nvme0n1,decoders:1
1a,2b,3c,4c,5d,6e,7f,/dev/nvme1,/dev/nvme1n1,decoders:2
2a,2b,2c,3,0,0,0,/dev/nvme0,/dev/nvme0n1,encoders:1
1,0,0,0,0,0,0,/dev/nvme1,/dev/nvme1n1,encoders:2
0,0,0,0,0,0,0,/dev/nvme0,/dev/nvme0n1,scalers:1
1,0,0,0,0,0,0,/dev/nvme1,/dev/nvme1n1,scalers:2

注意在 go 中为您的输入格式编写解析器应该不会那么困难

直接从 go 中您关心的文本开始怎么样?与使用 shell 实用程序相比,您在 go 中拥有更多的控制权。

这是一个小型状态机,用于查找前导文本“num”,以指示新项目的开始。下一行是标题,将被跳过,随后的行将转换为行,并将其添加到该项目中。在项目之间的边界以及输入文本/文件的末尾,最后一个项目将添加到所有项目的集合中。

package main

import (
    "bufio"
    "fmt"
    "regexp"
    "strings"
)

var txt = `
num item1: 2
index load model_load inst mem  share_mem p2p_mem device         namespace
1     2    3          4    50    600         700       1     1
1a     2b    3c          4c    5d    6e         7f       2     2
num item2: 2
index load model_load inst mem  share_mem p2p_mem device         namespace
2a     2b    2c          3    0    0         0       1     1
1     0    0          0    0    0         0       2     2
num item3: 1
index load model_load inst mem  share_mem p2p_mem device         namespace
i     iib    iic          iii    zero    zero         zero       i     i
**************************************************
`

var columns = regexp.mustcompile(`\s+`)

type row struct {
    index,
    load,
    model_load,
    inst_mem,
    share_mem,
    p2p_mem,
    device,
    namespace string
}

type item []row

func main() {
    r := strings.newreader(txt)
    scanner := bufio.newscanner(r)

    items := make([]item, 0)

    var item item
    for scanner.scan() {
        line := scanner.text()
        line = strings.trimspace(line)

        if len(line) == 0 ||
            strings.hasprefix(line, "***") {
            continue
        }

        // find beginning of an "item": if any previous item, save it and
        // reset item to append future rows; skip header line; continue
        if strings.hasprefix(line, "num item") {
            if len(item) > 0 {
                items = append(items, item)
                item = make(item, 0)
            }
            scanner.scan() // skip header
            continue
        }

        cols := columns.split(line, -1)
        row := row{cols[0], cols[1], cols[2], cols[3], cols[4], cols[5], cols[6], cols[7]}
        item = append(item, row)
    }

    // deal with last/trailing item
    if len(item) > 0 {
        items = append(items, item)
    }

    for i, item := range items {
        fmt.printf("item %d\n", i+1)
        for _, row := range item {
            fmt.println(row)
        }
    }
}

打印以下内容:

Item 1
{1 2 3 4 50 600 700 1}
{1a 2b 3c 4c 5d 6e 7f 2}
Item 2
{2a 2b 2c 3 0 0 0 1}
{1 0 0 0 0 0 0 2}
Item 3
{i iib iic iii zero zero zero i}

我不知道有更好的方法来创建结构,但它是直接的,而且相当干净。

今天关于《最有效的将shell输出绑定到Go结构的方法》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>