Sed 模式匹配和处理expect中的数据?

Sed 模式匹配和处理expect中的数据?,sed,awk,expect,cisco,Sed,Awk,Expect,Cisco,expect\u out(缓冲区) 我有上面的以下数据,我需要计算每种类型的数据数量 这样我就可以得到如下信息: GigabitEthernet1 : 20 GigabitEthernet2 : 20 Tel : 2 FastEthernet2 : 4 FastEthernet1 : 4 总数:50 我怎么做? 任何帮助都将不胜感激,因为就expect/tcl而言,我是个新手,所以我不知道该朝哪个方向前进 我尝试使用split函数通过使用换行符作为分隔符来解

expect\u out(缓冲区)

我有上面的以下数据,我需要计算每种类型的数据数量 这样我就可以得到如下信息:

GigabitEthernet1 : 20
GigabitEthernet2 : 20
Tel             : 2
FastEthernet2    : 4
FastEthernet1    : 4
总数:50

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

我尝试使用split函数通过使用换行符作为分隔符来解析它,以便在for循环中使用regex,但似乎因为$expect\u output(buffer)是一个变量,所以它可能没有任何行


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

根据您当前的输入数据,这一行:

 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

由于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
设置str$expect\u out(缓冲区)
#刀砍后把所有东西都剥掉
regsub-所有-行“/.*$str”str2
投入$str2#只是为了看看你到目前为止得到了什么
#将字符串转换为列表
设置li[拆分$str2”\n“]
#将列表转换为数组
#这实际上是将列表转换为
#其项必须首先设置为1的关联数组
#之后必须增加1
对于{set i 0}{$i<[llength$li]}{incr i}{
如果{[info exists arr([lindex$li$i])]}{
增量arr([lindex$li$i])){
设置arr([lindex$li$i])1}
}
#现在获取统计数据
数组获取arr
#我将打印此示例
#千兆以太网2 Te1 2快速以太网2 3千兆以太网1 9

你也应该用Tcl和TK来标记这个问题。

为什么
GigabitEthernet2:20
?我在你的输入中只看到其中两个。对不起,数据太大了,所以我把它拿出来了,我只是举个例子谢谢,@Kent但我还是希望有人能指导我使用expect,因为我的完整代码在expect中,但如果能在expect脚本中使用它,那就太好了:-)。我的意思是我可能必须设置它(expect\u out(buffer))然后作为环境变量,使用awk来进行这种处理
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