tags:

views:

80

answers:

4

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?

+1  A: 

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
Jackson
A: 

Hi Mallikarjunarao,

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

Fanatic23
A: 

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

RHSeeger
+3  A: 

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]
glenn jackman
+1: I can't improve on this answer.
Donal Fellows