2008/07/11

處理 NS2 Scenarios Generator 產生的 tcl

NS2 Scenarios Generator
是個不錯的 ns2 scenario 產生工具

但是產生出來的 tcl 可能在命名上不是我所慣用的

舉例來說

他會產生以下這種表示 node 的方式

[code]$n0 set X_ 907
$n0 set Y_ 975
$n0 set Z_ 0.0[/code]

但我比較習慣寫成類似 array 的方式

例如

[code]$node(0) set X_ 907
$node(0) set Y_ 975
$node(0) set Z_ 0.0[/code]

這個時候問題來了

如果有 1000 個點

總不可能一個一個手動改吧 囧

這個時候 vim 的搜尋取代功能就非常好用了

利用以下寫法就能一口氣換過來喔

:%s/\$n\([0-9]\+\)/$node(\1)/g


想要瞭解更多 vim 的用法請參考這篇文章

以下介紹我覺得最常用到的

1. 單純置換字串

:%s/old string/new string/g
以上用法會把檔案內所有的 old string 變成 new string

2. 利用 regular expression 置換字串

:%s/[0-9]\+/number/g
以上用法會把數字(ex: 1, 12, 130, ... etc)變成字串 number

:%s/a.b/string/g
以上用法會把字串 aab, abb, a0b, a9b, a!b, ... 等,a開頭b結尾,三個字為一組的字串變成字串 string

3. 利用 regular expression 把比對出的字串重新組合出新的字串置換

:%s/\(abc\) \(def\)/This is \1 & \2/g
以上用法會把字串 abc def 變成字串 This is abc & def



vim 功能非常強大

只要用習慣了

就會覺得非常方便喔

No comments:

Post a Comment