expect_out(buffer)
GigabitEthernet1/0/9 unassigned YES unset up up
GigabitEthernet1/0/10 unassigned YES unset down down
GigabitEthernet1/0/11 unassigned YES unset down down
GigabitEthernet1/0/23 unassigned YES unset down down
GigabitEthernet1/0/24 unassigned YES unset down down
GigabitEthernet1/1/1 unassigned YES unset down down
GigabitEthernet1/1/2 unassigned YES unset down down
GigabitEthernet1/1/3 unassigned YES unset down down
GigabitEthernet1/1/4 unassigned YES unset down down
Te1/1/1 unassigned YES unset down down
Te1/1/2 unassigned YES unset down down
FastEthernet2/0/1 unassigned YES unset down down
FastEthernet2/0/2 unassigned YES unset down down
FastEthernet2/0/24 unassigned YES unset down down
GigabitEthernet2/0/1 unassigned YES unset up up
GigabitEthernet2/0/2 unassigned YES unset down down
我上面有以下数据,我需要计算每种类型的数据数量 所以我可以得到如下信息:
GigabitEthernet1 : 20
GigabitEthernet2 : 20
Tel : 2
FastEthernet2 : 4
FastEthernet1 : 4
总共:50
我该怎么办? 任何帮助将不胜感激,因为我不知道在哪个方向继续,因为我是一个新手,就期望/ tcl而言。
我尝试使用split函数通过使用newline作为分隔符来解析它,这样我就可以在for循环中使用regex,但似乎因为$ expect_output(buffer)是一个变量,它可能没有任何行。
此外,我可以在内部使用awk或sed,然后我猜这不会那么困难。但预期的解决方案将是标准的。
答案 0 :(得分:2)
根据您当前的输入数据,这个单行:
awk -F'/' '{a[$1]++}END{for(x in a){print x" : "a[x];t+=a[x];}print "total : "t}' file
给出:
FastEthernet2 : 3
GigabitEthernet1 : 9
GigabitEthernet2 : 2
Te1 : 2
total : 16
答案 1 :(得分:1)
由于Expect基于 Tcl / TK ,因此您应该熟悉该语言,因为它包含许多字符串处理选项。这里有一些代码可以让你走上正轨。
set str $expect_out(buffer)
# Strip everything after slash
regsub -all -line "/.*" $str "" str2
puts $str2 # just to see what you got so far
# Convert string into list
set li [split $str2 "\n"]
# Convert list into array
# This is actually the tricky part which converts the list into an
# associative array whose entries have first to be set to one
# and later have to be increased by one
for {set i 0} {$i < [llength $li]} {incr i} {
if { [info exists arr([lindex $li $i]) ] } {
incr arr([lindex $li $i]) } {
set arr([lindex $li $i]) 1 }
}
# Now get the statistics
array get arr
# will print this for your example
# GigabitEthernet2 2 Te1 2 FastEthernet2 3 GigabitEthernet1 9
你应该用Tcl和TK标记这个问题。