02-21-2013 09:38 PM
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
Solved! Go to Solution.
02-21-2013 10:15 PM
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
02-21-2013 10:15 PM
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
02-22-2013 03:31 AM
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.
02-22-2013 10:04 AM
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide