如何使用 tcl 中的拆分删除不需要的字符 [英] How to remove unwanted charecters using split in tcl

查看:31
本文介绍了如何使用 tcl 中的拆分删除不需要的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个例子

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

我想在此删除 {}.
我假设所有上述字符串接口变量

I want remove {} in this.
I assumed all the above string interface variable

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

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

but it doesn't work. How to remove the {} in my example?

推荐答案

我怀疑你在这样做:从一个字符串开始并试图将它拆分成单词,只有 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]

这篇关于如何使用 tcl 中的拆分删除不需要的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆