user2643912
user2643912

Reputation: 11

grep string from a TCL variable

I want to grep a certain amount of string from a TCL variable and use that in my tool command. Example:

${tcl_Var} - this contains string like VEG_0_1/ABC

I want to grep from above string until it the point it hits first forward slash, so in the above case it would be VEG_0_1. And then replace it in my command. Example:

VEG_0_1/REST_OF_THE_COMMAND.

Upvotes: 1

Views: 1867

Answers (3)

user3837100
user3837100

Reputation: 1

set tcl_Var VEG_0_1/ABC  
set varlist [split $tcl_Var "/"]  
set newvar [lindex $varlist 0]/REST_OF_THE_COMMAND  

Upvotes: -1

glenn jackman
glenn jackman

Reputation: 246754

Don't think in terms of grep, think about "string manipulation" instead.

Use regsub for "search and replace:

% set tcl_Var VEG_0_1/ABC
VEG_0_1/ABC
% set newvar [regsub {/.+} $tcl_Var {/REST_OF_THE_COMMAND}]
VEG_0_1/REST_OF_THE_COMMAND

Alternately, your problem can be solved by splitting the string on /, taking the first component, then appending the "rest of the command":

% set newvar "[lindex [split $tcl_Var /] 0]/REST_OF_THE_COMMAND"
VEG_0_1/REST_OF_THE_COMMAND

Or using string indices:

% set newvar "[string range $tcl_Var 0 [string first / $tcl_Var]]REST_OF_THE_COMMAND"
VEG_0_1/REST_OF_THE_COMMAND

Upvotes: 3

pts
pts

Reputation: 87221

You can do this with regular expressions using the regsub TCL command. There is no need to run the external program grep. See more info here: http://www.tcl.tk/man/tcl8.4/TclCmd/regsub.htm

If you are new to regular expressions, read TCL-specific tutorial about them.

Upvotes: 0

Related Questions