首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TCL -捕捉前一行并进行比较

TCL -捕捉前一行并进行比较
EN

Stack Overflow用户
提问于 2016-01-26 15:51:21
回答 2查看 372关注 0票数 0

我有一个有内容的文件。

代码语言:javascript
复制
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通知更改删除”这两行,我需要将这两个命令放在分别的接口下面。

预期产出:

代码语言:javascript
复制
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命令时,脚本也会跳过它。

这是剧本。

代码语言:javascript
复制
    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

意外产出:

代码语言:javascript
复制
 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“来实现这一点是不合适的,但我没有找到其他方法。我如何纠正所有的错误?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-26 17:03:44

如果在两次传递中完成,这种文本解析就容易得多。下面的代码首先在字典中注册“接口”行,然后计算“接口”行后面的行数(0、1或2)。我假设如果下面有两行,一个是“添加”行,另一个是“删除”行。

在第二遍中,将遍历字典并打印0和1计数项。

代码语言:javascript
复制
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"
    }
}

文档:成龙字典前程如果打开看跌期权设置拆分字符串

票数 1
EN

Stack Overflow用户

发布于 2016-01-26 16:23:47

如果您一直想要相同的两行,那么您可以只查找interface行,跳过其余的行。

代码语言:javascript
复制
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_data
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35018038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档