0

How can i use variable inside grep in exec in TCL

"^STRING__${abc}__STRING__${abc}\[\[:space:\]\]*="

Tried Ways,

exec grep "^STRING__${abc}__STRING__${abc}\[\[:space:\]\]*="
exec grep {^STRING__${abc}__STRING__${abc}\[\[:space:\]\]*=}
exec grep {{^STRING__${abc}__STRING__${abc}\[\[:space:\]\]*=}}

Tried this above solutions, but not able to execute properly.

Thanks

1 Answer 1

3

I take it that your grep requires the brackets to be escaped? The first Way above will strip away the backslashes, and the second and third won't substitute the variable.

The easiest way to do this is probably to use format:

set regex [format {^STRING__%1$s__STRING__%1$s\[\[:space:\]\]*=} $abc]
exec grep $regex

The principle of it is to write the regex string as you want it to be inside the braces and replace the variable occurrences with %s specifiers, or %1$s to put the same string in more than one place, add the string to be inserted and call format on it.

If you don't need the backslashes after all, it's safe to remove them from the format string (as long as the braces are in place around it).

Documentation: format, set

Sign up to request clarification or add additional context in comments.

1 Comment

FYI: As of today, i was able to follow @David W. answer to a similar Question: stackoverflow.com/questions/18147884/… That solved the problem by double-quoting the variable that contains the regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.