我正在学习Go并且正在尝试构建一个监视许可证使用情况的工具。
我能够运行命令OK并将输出存储到文件中,我现在要做的是从下面的输出中分割出关键数据。
作为示例,我运行命令,它将返回此示例输出。
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)
我想遍历每一行并获取功能名称“%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)
}
任何提示?
感谢
答案 0 :(得分:3)
试试这个。
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])
}
}
}
}