gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:22:48 25 4
gpt4 key购买 nike

举个例子

Interface {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} IP-Address {} {} {} {} {} OK? Method Status {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {Protocol
FastEthernet0/0} {} {} {} {} {} {} {} {} {} {} {} unassigned {} {} {} {} {} YES unset {} administratively down down {} {} {} {
FastEthernet0/1} {} {} {} {} {} {} {} {} {} {} {} unassigned {} {} {} {} {} YES unset {} administratively down down

我想删除其中的 {}
我假定所有上述字符串接口(interface)变量

set interface [string trimright [string trimleft $interface "{}"] "{}"]

但它不起作用。如何删除示例中的 {}

最佳答案

我怀疑您正在这样做:从一个字符串开始并试图将其拆分为单词,只有 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 {} {} {} ...

Tcl 的 split 默认在单个空白字符上拆分(不像 awk 或 perl 在连续的空白字符上拆分)。

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

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

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

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

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

关于tcl - 如何使用 tcl 中的拆分删除不需要的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3020690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com