Splitting strings in Golang

huangapple go评论88阅读模式
英文:

Splitting strings in Golang

问题

我正在学习Go,并尝试构建一个监控许可证使用情况的工具。

我能够成功运行命令并将输出存储到文件中,现在我想做的是从下面的输出中提取关键数据。

举个例子,我运行命令后会返回以下示例输出。

lmutil - 版权所有 (c) 1989-2015 Flexera Software LLC. 保留所有权利。
Thu 8/17/2017 12:18 上的 Flexible License Manager 状态

[正在检测 lmgrd 进程...]
许可证服务器状态: 28000@servername
位于 servername 上的许可证文件: C:\Program Files\autocad\LM\autocad.lic:

servername: 许可证服务器已启动 (MASTER) v11.13.1

servername 上的厂商守护程序状态:

autocad: 已启动 v11.13.1
功能使用信息:

autocad 的用户:  (共发出 1 个许可证;  当前使用 0 个许可证)

feature2 的用户:  (共发出 1000 个许可证;  当前使用 0 个许可证)

我想循环遍历每一行,并提取特征名称"Users of %feature%",发出的许可证总数和当前使用的许可证总数。

我知道在Python中可以使用类似以下的代码:

for line in output.splitlines(true):

但是在Go中,我只做到了这一步:

func splitOutput(outs []byte) {
    outputStr := string(outs[:])
    split := strings.Split(outputStr, "\n")
    fmt.Printf("start split: \n", split)
}

有什么建议吗?

谢谢。

英文:

I am learning Go and am attempting to build a tool to monitor license usage.

I'm able to run the command OK and store the output to file fine, what I'd like to do now is split key data out of the below output.

As an example, I run the command and it will return this example output.

lmutil - Copyright (c) 1989-2015 Flexera Software LLC. All Rights Re
served.
Flexible License Manager status on Thu 8/17/2017 12:18

[Detecting lmgrd processes...]
License server status: 28000@servername
License file(s) on servername: C:\Program Files\autocad\LM\autocad.lic:

servername: license server UP (MASTER) v11.13.1

Vendor daemon status (on servername):

autocad: UP v11.13.1
Feature usage info:

Users of autocad:  (Total of 1 license issued;  Total of 0 licenses in use)

Users of feature2:  (Total of 1000 licenses issued;  Total of 0 licenses in us
e)

I'd like to loop through each line and grab the feature name 'Users of %feature%', total of licenses issued and total in use.

I know in python I can use something like

for line in output.splitlines(true):

but this is about as far as I've gotten in Go.

func splitOutput(outs []byte) {
	outputStr := string(outs[:])
	split := strings.Split(outputStr, "\n")
	fmt.Printf("start split: \n", split)
}

Any tips?

thanks

答案1

得分: 3

尝试这个。

func splitOutput(outs []byte) {
    outputStr := string(outs[:])
    split := strings.Split(outputStr, "\n")
    fmt.Printf("分割结果:%q\n", split)
    for index, line := range split {
        fmt.Printf("第 %d 行:%s\n", index, line)
        if len(line) >= 9 && line[0:9] == "Users of " {
            lineSplit := strings.Split(line, " ")
            if len(lineSplit) == 16 {
                name := lineSplit[2]
                name = name[0:len(name) - 1]
                fmt.Printf("%s %s %s\n", name, lineSplit[6], lineSplit[12])
            }
        }
    }
}

在线测试:https://play.golang.org/p/m6JIBytU0m

英文:

Try this.

func splitOutput(outs []byte) {
    outputStr := string(outs[:])
    split := strings.Split(outputStr, "\n")
    fmt.Printf("Splitted result: %q\n", split)
    for index, line := range split {
        fmt.Printf("Line %d: %s\n", index, line)
        if len(line) >= 9 && line[0:9] == "Users of " {
            lineSplit := strings.Split(line, " ")
            if len(lineSplit) == 16 {
                name := lineSplit[2]
                name = name[0:len(name) - 1]
                fmt.Printf("%s %s %s\n", name, lineSplit[6], lineSplit[12])
            }
        }
    }
}

Test it online: https://play.golang.org/p/m6JIBytU0m

huangapple
  • 本文由 发表于 2017年8月17日 10:40:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/45725795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定