且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-05-18 09:12:52

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

I suspect you're doing this: starting with a string and trying to split it into words, only Tcl's split command is producing a list with lots of empty values:

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 {} {} {} ...

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

Tcl's split splits on individual whitespace characters by default (unlike awk or perl that splits on consecutive whitespace chars).

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

You can some choices to make your life easier:

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

1) use regexp to find all "words"

set fields [regexp -inline -all {\S+} $input] 

2) 将 textutil 包用于拆分命令,其行为与您预期的一样:

2) use the textutil package for a split command that acts like you seem to expect:

package require textutil
set fields [textutil::splitx $input]