我有一个有内容的文件。
file.txt
interface FastEthernet0/20
snmp trap mac-notification change added
interface FastEthernet0/21
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface FastEthernet0/22
snmp trap mac-notification change removed
interface FastEthernet0/23
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface FastEthernet0/24
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface GigabitEthernet0/1
interface GigabitEthernet0/2我制作了一个脚本来读取这个文件,如果接口没有将"snmp陷阱mac通知更改添加“和"snmp陷阱mac通知更改删除”这两行,我需要将这两个命令放在分别的接口下面。
预期产出:
conf t
interface FastEthernet0/20
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface FastEthernet0/21
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface FastEthernet0/22
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface GigabitEthernet0/1
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface GigabitEthernet0/2
snmp trap mac-notification change added
snmp trap mac-notification change removed这个脚本有两个问题。一种是,如果接口中只有一行"snmp陷阱mac通知更改已删除“,则脚本跳过该块并转到下一个接口,直到找到包含三个命令(接口、snmp添加和snmp删除)的块,或者当没有两个snmp命令时。另一个问题是,当最后一行“接口GigabitEthernet0 0/2”没有snmp命令时,脚本也会跳过它。
这是剧本。
set intf {}
set PR {}
set PPR {}
set PPRfullintf {}
set PPRintf {}
set PRfullintf {}
set PRintf {}
set 4 {}
set P4 {}
set f [open "file.txt" r]
foreach a [split [read -nonewline $f] \n] {
set 1 [lindex $a 1]
set 0 [lindex $a 0 ]
set 4 [lindex $a 4 ]
if { [regexp {^Fa|^Gi} $1] } { set intf1 "interface $1" }
if { $4 != "added" && $4 != "removed" && $P4 == "added" && $PR == "interface" } {
puts "conf t\r"
puts "$PRfullintf\r"
puts "snmp trap mac-notification change added\r"
puts "snmp trap mac-notification change removed\r"
}
if { $0 == "interface" && $PR == "interface" } {
puts "conf t\r"
puts "$PRfullintf\r"
puts "snmp trap mac-notification change added\r"
puts "snmp trap mac-notification change removed\r"
}
set P4 $4
if { $4 == "added" || $4 == "removed" } { set P4 $4 }
set PR $0
set PRfullintf $intf1
set PRintf $1
}
close $f意外产出:
conf t
interface FastEthernet0/20
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface FastEthernet0/21
snmp trap mac-notification change added
snmp trap mac-notification change removed
conf t
interface GigabitEthernet0/1
snmp trap mac-notification change added
snmp trap mac-notification change removed我认为试图使用命令"foreach“来实现这一点是不合适的,但我没有找到其他方法。我如何纠正所有的错误?
谢谢。
发布于 2016-01-26 17:03:44
如果在两次传递中完成,这种文本解析就容易得多。下面的代码首先在字典中注册“接口”行,然后计算“接口”行后面的行数(0、1或2)。我假设如果下面有两行,一个是“添加”行,另一个是“删除”行。
在第二遍中,将遍历字典并打印0和1计数项。
set f [open file.txt]
foreach line [split [string trim [chan read $f]] \n] {
if {[string match interface* $line]} {
set key $line
dict set interfaces $key 0
} else {
dict incr interfaces $key
}
}
chan close $f
dict for {key n} $interfaces {
if {$n < 2} {
puts "conf t"
puts $key
puts "snmp trap mac-notification change added"
puts "snmp trap mac-notification change removed"
}
}文档:成龙,字典,前程,如果,打开,看跌期权,设置,拆分,字符串
发布于 2016-01-26 16:23:47
如果您一直想要相同的两行,那么您可以只查找interface行,跳过其余的行。
set data {
interface FastEthernet0/20
snmp trap mac-notification change added
interface FastEthernet0/21
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface FastEthernet0/22
snmp trap mac-notification change removed
interface FastEthernet0/23
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface FastEthernet0/24
snmp trap mac-notification change added
snmp trap mac-notification change removed
interface GigabitEthernet0/1
interface GigabitEthernet0/2
}
set data [split $data "\n"]
set new_data ""
foreach {line} $data {
if {[string first "interface" $line] != -1} {
# Found an interface. Add to new_data
append new_data "conf t\n$line\n"
append new_data "snmp trap mac-notification change added\n"
append new_data "snmp trap mac-notification change removed\n"
}
}
puts $new_datahttps://stackoverflow.com/questions/35018038
复制相似问题