-1

的内容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

我该怎么做?任何帮助将不胜感激,因为我不知道该朝哪个方向前进,因为就 expect/tcl 而言,我是新手。

我尝试使用 split 函数通过使用换行符作为分隔符来解析它,以便我可以在 for 循环中使用正则表达式,但似乎因为 $expect_output(buffer) 是一个变量,它可能没有任何行。

此外,我可以在 expect 中使用 awk 或 sed,那么我猜它不会那么困难。但预期的解决方案将是标准的。

4

2 回答 2

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
于 2013-03-07T15:56:46.940 回答
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 标记这个问题。

于 2013-03-08T14:11:08.763 回答