2015-12-02 80 views
0

更改VLAN號,我需要改變一個VLAN號1到555的列表上象下面這樣:在file.txt的

file.txt的

SW1#sh interfaces status 

Port  Name    Status  Vlan  Duplex Speed Type 
Fa0/1      connected trunk  full 100 10/100BaseTX 
Fa0/2      notconnect 1   auto auto 10/100BaseTX 
Fa0/3      notconnect 1   auto auto 10/100BaseTX 
Fa0/4      notconnect 1   auto auto 10/100BaseTX 
Fa0/5      connected 1   a-full a-100 10/100BaseTX 
Fa0/6      notconnect 1   auto auto 10/100BaseTX 
Fa0/7      connected 1   a-half a-10 10/100BaseTX 
Fa0/8      connected 1   a-full a-100 10/100BaseTX 
Fa0/9      notconnect 1   auto auto 10/100BaseTX 
Fa0/10      notconnect 1   auto auto 10/100BaseTX 
Fa0/11      connected 1   a-full a-100 10/100BaseTX 
Fa0/12      notconnect 1   auto auto 10/100BaseTX 
Fa0/13      notconnect 1   auto auto 10/100BaseTX 
Fa0/14      notconnect 1   auto auto 10/100BaseTX 
Fa0/15      notconnect 1   auto auto 10/100BaseTX 
Fa0/16      notconnect 1   auto auto 10/100BaseTX 
Fa0/17      notconnect 1   auto auto 10/100BaseTX 
Fa0/18      notconnect 1   auto auto 10/100BaseTX 
Fa0/19      notconnect 1   auto auto 10/100BaseTX 
Fa0/20      notconnect 1   auto auto 10/100BaseTX 
Fa0/21      notconnect 1   auto auto 10/100BaseTX 
Fa0/22      notconnect 1   auto auto 10/100BaseTX 
Fa0/23      notconnect 1   auto auto 10/100BaseTX 
Fa0/24 TRUNK VOICE BRANCH connected 100  a-full a-100 10/100BaseTX 
Gi0/1      connected trunk  a-full a-1000 10/100/1000BaseTX 
Gi0/2      notconnect 1   auto auto Not Present 

代碼:

set f [open "file.txt" r] 
    foreach a [split [read -nonewline $f] \n] { 
    set 0 [lindex $a 0] 
    set b [lsearch -inline -regexp $a "1"] 

    if { [regexp {^F|^G|^P} $0] && $b == "1"} { 
     puts "conf t" 
     puts "interface $0" 
     puts "switchport access vlan 555" 
    } 
} 


Output: 

conf t 
interface Fa0/2 
switchport access vlan 555 
conf t 
interface Fa0/3 
switchport access vlan 555 
conf t 
interface Fa0/4 
switchport access vlan 555 
conf t 
interface Fa0/5 
switchport access vlan 555 
conf t 
interface Fa0/6 
switchport access vlan 555 
conf t 
interface Fa0/7 
switchport access vlan 555 
conf t 
interface Fa0/8 
switchport access vlan 555 
conf t 
interface Fa0/9 
switchport access vlan 555 
conf t 
interface Fa0/20 
switchport access vlan 555 
conf t 
interface Fa0/22 
switchport access vlan 555 
conf t 
interface Fa0/23 
switchport access vlan 555 
conf t 
interface Gi0/2 
switchport access vlan 555 

你怎麼看,端口Fa0/10到Fa0/19和端口Fa0/21沒有被配置。我試着用其他方法改變研究,但直到現在我失敗了。

我期望的正確輸出是所有端口與vlan 1我需要更改爲vlan 555.我該如何做到這一點?

Tkanks。

+0

我相信 「1」 碰壁您的正則表達式搜索上1對10-19和21.嘗試使用'{\ m1 \ M}'代替。 –

+0

謝謝彼得!盡我所需! –

回答

1

問題是,lsearch -inline -regexp $a "1"找到任何類型的字符串中有一個「一個」數字,例如19個。稍後,當這與$b == "1"中的1進行比較時,這將變爲19 == 1,失敗。

解決的辦法是限制對手的確切字符串1,通過使用幾種可能的正則表達式之一:{\m1\M}之一,但{\y1\y}{^1$}工作過。

請注意,這段代碼仍然有些脆弱(例如,第一行的匹配,僅僅是巧合)。

if {[string match {[FGP]*} $a] && [string range $a 42 52] == 1}可能會更好。

文檔:iflsearchSyntax of Tcl regular expressionsstring

(請參閱 「約束逃逸」 的正則表達式語法文檔。)