cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3049
Views
0
Helpful
3
Replies

foreach command help

Cory Anderson
Level 1
Level 1

Hi,

I'm trying to understand how I can run an lindex against multiple lines.  I've read both 'foreach' and 'for' man page and foreach sounds like the only option.  That said I don't think I'm close to getting this working.   

Also is there a better forum for TcL specific Q&A's?  The end result will be for the IOS, but I need to learn how to manipulate text a little more. 

An example of what I'm trying to do is:

set ARP [exec "show ip arp | i Internet"]

Internet  10.0.0.1                -   588e.09a4.e0d8  ARPA   Vlan4

Internet  10.0.0.2               10   c571.fe20.4ba02  ARPA   Vlan4

Internet  10.0.0.13              53   38ba.dc1c.b7f7  ARPA   Vlan4

foreach IP {$ARP} {

lindex $IP 1

}

10.0.0.1

10.0.0.2

10.0.0.13

foreach MAC {$ARP} {

lindex $MAC 3

}

588e.09a4.e0d8

c571.fe20.4ba02

38ba.dc1c.b7f7

then maybe lappend each line to get:

$IP $MAC to get:

10.0.0.1 588e.09a4.e0d8

10.0.0.2 c571.fe20.4ba02

10.0.0.13 38ba.dc1c.b7f7

1 Accepted Solution

Accepted Solutions

mtimm
Cisco Employee
Cisco Employee

Hi Cory,

I think the key piece of info you may be missing is the need to split the command output by newline breaks:

set ARP [split [exec "show ip arp | i Internet"] "\n"]

This will create a list out of the command output where each line of the command output is an element in the list.  That way when you do the foreach on $ARP, you are not operating on each individual piece of the command output but on each line of the command output.  So then you could then combine the lindex commands to extract the IP address and mac addresses in one fell swoop:

foreach line $ARP {

    puts "[lindex $line 1] [lindex $line 3]"

}

To further drive home the point, try these examples and compare the output:

set ARP [exec "show ip arp | i Internet"]

foreach element $ARP {

    puts $element

}

set ARP [split [exec "show ip arp | i Internet"] "\n"]

foreach element $ARP {

    puts $element

}

Note that I have not tested any of this on an actual IOS device but I'm fairly certain splitting the output by newline and processing the output by lines rather than by space separated pieces should be closer to what you want if I understood your question correctly.

Mike

View solution in original post

3 Replies 3

mtimm
Cisco Employee
Cisco Employee

Hi Cory,

I think the key piece of info you may be missing is the need to split the command output by newline breaks:

set ARP [split [exec "show ip arp | i Internet"] "\n"]

This will create a list out of the command output where each line of the command output is an element in the list.  That way when you do the foreach on $ARP, you are not operating on each individual piece of the command output but on each line of the command output.  So then you could then combine the lindex commands to extract the IP address and mac addresses in one fell swoop:

foreach line $ARP {

    puts "[lindex $line 1] [lindex $line 3]"

}

To further drive home the point, try these examples and compare the output:

set ARP [exec "show ip arp | i Internet"]

foreach element $ARP {

    puts $element

}

set ARP [split [exec "show ip arp | i Internet"] "\n"]

foreach element $ARP {

    puts $element

}

Note that I have not tested any of this on an actual IOS device but I'm fairly certain splitting the output by newline and processing the output by lines rather than by space separated pieces should be closer to what you want if I understood your question correctly.

Mike

Totally agree, I love the split command

Also, be sure to do it how Mike's syntax is with the ARP variable.

Dont place $ARP in brackets {$ARP} as this will mean no substitution will occur.

Cory Anderson
Level 1
Level 1

Thanks Mike!, Matt!,

I think I understand now.  The split makes the original output split into multiple lines with "\n", otherwise the new line is treated as a white space on the first line.  You just made TcL a lot more useful for me.  Split \n will be used in all kinds of scenarios, which I will try to upload to the EEM community if I have something that maybe valuable to others.

Thank you,

Cory   

Review Cisco Networking for a $25 gift card