cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Bookmark
|
Subscribe
|
6023
Views
0
Helpful
4
Replies

Cut command in TcL?

Cory Anderson
Level 1
Level 1

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

1 Accepted Solution

Accepted Solutions

mtimm
Cisco Employee
Cisco Employee

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

View solution in original post

4 Replies 4

mtimm
Cisco Employee
Cisco Employee

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

Thanks Mike,

I'll give this a shot.  What does the 1 in "% set hostName [lindex [split $Name " "] 1]" signify? 

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

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.