如何使用tcl中的split删除不需要的字符

Mal*_*uri 2 tcl

这是一个例子

Interface {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} IP-Address {} {} {} {} {} OK? Method Status {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {Protocol
FastEthernet0/0} {} {} {} {} {} {} {} {} {} {} {} unassigned {} {} {} {} {} YES unset {} administratively down down {} {} {} {
FastEthernet0/1} {} {} {} {} {} {} {} {} {} {} {} unassigned {} {} {} {} {} YES unset {} administratively down down
Run Code Online (Sandbox Code Playgroud)

我想删除{}这个.
我假设所有上面的字符串接口变量

set interface [string trimright [string trimleft $interface "{}"] "{}"]
Run Code Online (Sandbox Code Playgroud)

但它不起作用.如何{}在我的例子中删除?

gle*_*man 6

我怀疑你是这么做的:从字符串开始并尝试将其拆分为单词,只有Tcl的split命令产生一个包含大量空值的列表:

set input "Interface                  IP-Address      OK? Method Status                ProtocolFastEthernet0/0            unassigned      YES unset  administratively down down    FastEthernet0/1            unassigned      YES unset  administratively down down"
set fields [split $input]  ;# ==> Interface {} {} {} ...
Run Code Online (Sandbox Code Playgroud)

split默认情况下,Tcl 对单个空格字符进行拆分(与在连续的空白字符上拆分的awk或perl不同).

您可以选择一些让您的生活更轻松的选择:

1)使用正则表达式查找所有"单词"

set fields [regexp -inline -all {\S+} $input] 
Run Code Online (Sandbox Code Playgroud)

2)使用textutil包进行拆分命令,其行为与您期望的一样:

package require textutil
set fields [textutil::splitx $input]
Run Code Online (Sandbox Code Playgroud)