如何在 tcl 中使用 split 删除不需要的字符
示例
Interface {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} IP-Address {} {} {} {} {} OK? Method Status {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {Protocol
FastEthernet0/0} {} {} {} {} {} {} {} {} {} {} {} unassigned {} {} {} {} {} YES unset {} administratively down down {} {} {} {
FastEthernet0/1} {} {} {} {} {} {} {} {} {} {} {} unassigned {} {} {} {} {} YES unset {} administratively down down
这是我想要删除其中的 {}
的 。
我假设了上述所有字符串接口变量
set interface [string trimright [string trimleft $interface "{}"] "{}"]
,但它不起作用。如何删除我的示例中的 {}
?
Here is an example
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是你可以做的:
这是结果:
Arpan
Here's what you could do:
And here's the result:
Arpan
我同意杰克逊的观点,您的数据非常像是一个列表(结构化数据)。你能确定数据的结构是什么吗?如果可以,那么您可以获取列表中的数据并正确解释这些值...并且 {} 部分很可能是没有值的字段。对于(简化的)示例:
显然,数据和代码有点后悔(我使用了非常简单的代码来让我在做什么变得显而易见),但这个想法应该是可以理解的。
话虽如此,我“感觉”你的数据在某种程度上是“错误的”......就像有几个地方缺少大括号(例如 FastEthernet0/0 和 0/1 都应该是 Protocol 的子级) , ETC。)
I agree with Jackson that your data very much appears to be a list (structured data). Can you identify what the structure of the data is? If you can, then you can take the data in the list and interpret the values correctly... and the {} pieces are very likely fields that don't have a value. For (a simplified) example:
Obviously, the data and code is a bit contrite (I used very simple code to keep it obvious what I was doing), but the idea should be understandable.
All that being said, it "feels" to me like your data is "wrong" in some way... like there's a missing curly brace in there a couple places (like FastEthernet0/0 and 0/1 should both be children of Protocol, etc.)
你所看到的看起来像是 TCL 列表而不是字符串。因此,将数据视为列表,您可以这样做:
What you have there look like a TCL list rather than a string. So treating the data as a list you can something like this:
我怀疑你正在这样做:从一个字符串开始并尝试将其拆分为单词,只有 Tcl 的
split
命令生成一个包含大量空值的列表:Tcl's
split
默认情况下按单个空白字符拆分(与按连续空白字符拆分的 awk 或 perl 不同)。您可以选择一些让您的生活更轻松的选择:
1)使用 regexp 查找所有“单词”
2)使用 textutil 包作为 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: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) use regexp to find all "words"
2) use the textutil package for a split command that acts like you seem to expect: