- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2013 07:29 PM
Hi,
I was wondering if there is a cut command in TcL. I'm trying to accomplish this:
# set variable
set Name [exec "show run | grep hostname | cut -d" " -f2"]
# Show Hostname
puts $Name
Router_Hostname
I can do this, but can I cross between IOS.sh & TcL.sh?
R1#terminal shell
R1#show run | grep "^hostname" | cut -d" " -f2
R1
Solved! Go to Solution.
- Labels:
-
EEM Scripting
Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2013 09:48 PM
There is no cut command really but there are ways to extract what you want. If I had to do it in Tcl exclusively I'd probably use a combination of the split Tcl command and the lindex Tcl command like this:
% set Name "hostname Router"
hostname Router
% set hostName [lindex [split $Name " "] 1]
Router
%
The premise is to split the string by the space which returns a list then ask for the item in that list at index 1.
Mike

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2013 09:48 PM
There is no cut command really but there are ways to extract what you want. If I had to do it in Tcl exclusively I'd probably use a combination of the split Tcl command and the lindex Tcl command like this:
% set Name "hostname Router"
hostname Router
% set hostName [lindex [split $Name " "] 1]
Router
%
The premise is to split the string by the space which returns a list then ask for the item in that list at index 1.
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2013 10:19 AM
Thanks Mike,
I'll give this a shot. What does the 1 in "% set hostName [lindex [split $Name " "] 1]" signify?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2013 11:17 AM
Hi Cory,
The 1 is an index number to say which item of the list you are requesting. Lists in Tcl are zero based indexed, so in this example item 0 will be the word "hostname" and item 1 would be the actual hostname of the device.
I guess one key point that may not be totally obvious is that the square brackets are used for command substitution and everything within the brackets are treated as a single command. Also everything within and including the brackets is substituted with the result of that command. Because of this substitution, commands can be nested as seen here. The innter ommands are done first, the results are then substituted into the outter commands. So in my example the split command is executed on the Name variable the result is substituted into the lindex command. Then the lindex command is executed and the result is substituted into the set command.
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2013 01:45 PM
Mike,
Thank you for the clear explanation. I'm trying to get the hang of this. I wrote an applet that uses an tracked object to trigger a backup. I know that I can use the archive command with the $h & write-memory, but I don't want the clear text scp username & password in the configuration. So I converted this app to a TcL using the automated tool here, then set the variables in the TcL using:
set NAME_CMD [exec "show run | i ^hostname"]
set HOSTNAME [lindex [split $NAME_CMD " "] 1]
set SERVER "10.1.1.1"
set USER "admin"
set PASSWORD "password"
I think I'm having a problem with "copy run scp://$USER:$PASSWORD@$SERVER/$HOSTNAME.cfg"
Is this a syntax problem with the special characters? If I put the variables in brackets or quotes, it still doesn't seem to work.
