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

发布于 2024-09-05 15:04:11 字数 639 浏览 4 评论 0原文

示例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟花易冷人易散 2024-09-12 15:04:17

这是你可以做的:

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

set y [regsub -all ({}\ )+ $y ""]

这是结果:

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

Arpan

Here's what you could do:

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

set y [regsub -all ({}\ )+ $y ""]

And here's the result:

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

Arpan

我同意杰克逊的观点,您的数据非常像是一个列表(结构化数据)。你能确定数据的结构是什么吗?如果可以,那么您可以获取列表中的数据并正确解释这些值...并且 {} 部分很可能是没有值的字段。对于(简化的)示例:

set data {
    Person Steve Smith employee
    Position employee Employee 40
}

while {[llength $data] > 0} {
    set type [lindex $data 0]
    set data [lrange $data 1 end]
    switch -exact -- $type {
        Person {
            set first_name [lindex $data 0]
            set last_name [lindex $data 1]
            set position_shortname [lindex $data 2]
            set data [lrange $data 3 end]
        }
        Position {
            set shortname [lindex $data 0]
            set hourperweek [lindex $data 1]
            set data [lrange $data 2 end]
        }
        default {
            error "Unknown data type $type"
        }
    }
}

显然,数据和代码有点后悔(我使用了非常简单的代码来让我在做什么变得显而易见),但这个想法应该是可以理解的。

话虽如此,我“感觉”你的数据在某种程度上是“错误的”......就像有几个地方缺少大括号(例如 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:

set data {
    Person Steve Smith employee
    Position employee Employee 40
}

while {[llength $data] > 0} {
    set type [lindex $data 0]
    set data [lrange $data 1 end]
    switch -exact -- $type {
        Person {
            set first_name [lindex $data 0]
            set last_name [lindex $data 1]
            set position_shortname [lindex $data 2]
            set data [lrange $data 3 end]
        }
        Position {
            set shortname [lindex $data 0]
            set hourperweek [lindex $data 1]
            set data [lrange $data 2 end]
        }
        default {
            error "Unknown data type $type"
        }
    }
}

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.)

为你鎻心 2024-09-12 15:04:14

你所看到的看起来像是 TCL 列表而不是字符串。因此,将数据视为列表,您可以这样做:

set data [list a b {} {} e f {} {} g {}]
puts $data

set res {}
foreach ele $data {
    if { $ele != {}} {lappend res $ele}
}

puts $res

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:

set data [list a b {} {} e f {} {} g {}]
puts $data

set res {}
foreach ele $data {
    if { $ele != {}} {lappend res $ele}
}

puts $res
我很OK 2024-09-12 15:04:13

我怀疑你正在这样做:从一个字符串开始并尝试将其拆分为单词,只有 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's split默认情况下按单个空白字符拆分(与按连续空白字符拆分的 awk 或 perl 不同)。

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

1)使用 regexp 查找所有“单词”

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

2)使用 textutil 包作为 split 命令,其行为就像您期望的那样:

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

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'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"

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

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

package require textutil
set fields [textutil::splitx $input]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文