The problem is that you're wanting to mix variables from Tcl and variables from Awk. That's a bit complicated, as they are necessarily separate processes.
One option is to use Tcl's format
command to build the Awk script-let.
set a 0
incr a
foreach i {0.. 11} { # i variable is used for another thing
set awkcode [format {NR==%d {print $1}} $a]
set test [exec awk $awkcode clk_gate_names]
}
However, a better approach is to pass the variable in by the -v
option. That has the advantage of being not easily confused by any weird quoting going on, and Tcl doesn't break words unexpectedly (except, in exec
, when it looks like a file/pipe redirection; that's a wart).
set a 0
incr a
foreach i {0.. 11} { # i variable is used for another thing
set test [exec awk -v a=$a {NR==a {print $1}} clk_gate_names]
}
The third way is to use an environment variable. That has the advantage of not being susceptible to the problems with redirections (so you can pass in values containing >
for example).
set a 0
incr a
foreach i {0.. 11} { # i variable is used for another thing
set env(A_FROM_TCL) $a
set test [exec awk {NR==$ENVIRON["A_FROM_TCL"] {print $1}} clk_gate_names]
}
Notes:
- Remember with awk and Tcl, don't use single quotes. Tcl doesn't use the same syntax as bash. Put the script in braces always or you'll get very confused.
- I assume you know about the syntax of
foreach
? For the purposes of our discussion here it's not important, but you'd probably be having problems from it if the code you posted is exactly what you are using…