【发布时间】:2016-04-21 21:20:28
【问题描述】:
我正在尝试编写一个可以解析以下输出的脚本 -
dev1# show stats all
20:01:02-180 (stats) ID=1a2b3c
Work Stats -- Now -- -------- Overall --------
T1 T2 Total T5 T6 Total
container 3 3 0 3 3 3
operatioms 3 3 0 3 3 3
docker 3 3 0 3 3 3
tcl 3 3 0 3 3 3
app 3 3 0 3 3 3
external 1 4 0
intra 2 6 0
incoming locks 8 6 0
outgoing locks 4 3 0
race-condition times 10 20 23
threads/usage 45 56 70
Power 2.3 10
Consumption 20.3% 29%
-----------------------------------------------------------------------
Separate Command
-----------------------------------------------------------------------
dev1# show usage
20:01:08-100
OS: 48270 %
Core: 4524 %
User: 90 %
很遗憾,设备输出的格式不正确。
在浏览 tcl 博客时,我发现一个博客有以下代码 -
set input [dev1 run "show stats"]
array unset output
array set output {}
foreach line [split $input "\n"] {
if {[regexp {^([^:]+?)\s*:\s*(\S+)\s*(\S+)?$} $line ]} {
set key [string tolower $key 0 0]
set output($key) $value
if {[string length $units]} {
set output(${key}Unit) $units
}
}
}
foreach {key value} [array get output] {
puts [list $key $value]
}
我无法让它工作。虽然,遵循博客步骤。有人可以指出一些提示来解决这个问题。我是论坛的新手,想了解更多信息。
预期输出
stats {
{time 20:01:02-180}
{id 1a2b3c}
{cmd stats}
{Now
{T1 {container 3} {operatioms 3} {docker 3} {tcl 3} {app 3} {extrenal 3} {intra 2} {incoming_locks 8} {outgoing_locks 4} {race-condition_times 10} {threads/usage 45}}
{T2 {container 3} {operatioms 3} {docker 3} {tcl 3} {app 3} {extrenal 4} {intra 6} {incoming_locks 6} {outgoing_locks 3} {race-condition_times 20} {threads/usage 56}}
{Total {container 3} {operatioms 3} {docker 3} {tcl 3} {app 3} {extrenal 4} {intra 6} {incoming_locks 0} {outgoing_locks 0} {race-condition_times 23} {threads/usage 70}}
}
{Overall
{T5 {container 3} {operatioms 3} {docker 3} {tcl 3} {app 3} }
{T6 {container 3} {operatioms 3} {docker 3} {tcl 3} {app 3} }
{Total {container 3} {operatioms 3} {docker 3} {tcl 3} {app 3} }
}
{Power {current 2.3} {total 10}}
{Consumptiomn {current 20.3%} {total 29%}}
}
-----------------------------------------------------------------------
-----------------------------------------------------------------------
usage {
{time 20:01:08-100}
{OS 48270%}
{Core 4524%}
{User 90%}
}
谢谢
【问题讨论】:
-
欢迎来到 StackOverflow!为了更好地回答您的问题,请编辑您的问题并指定您无法使用的内容。代码是否正在运行,是否解析了部分但不是全部的输出,是否解析不正确,等等。
-
感谢 @buczek 的 cmets。我已经更新了问题。
-
您应该查看您的正则表达式,它不会解析输入中的所有行。为相似的数据添加更多正则表达式并捕获所需的值
-
您必须编写自己的解析器才能获得所需的输出。单个正则表达式将无法检索到这样的所有内容(除非您将其变得荒谬且难以理解)。
标签: tcl