2

I need to pass a variable value to awk command used in tickle. Goal : i want to extract fisrt field of file clk_gate_names one by one in loop.So this is small example shown the same concept have benn used in one script.

one testcase below:

set a 0
incr a
foreach i {0.. 11} { # i variable is used for another thing
set test [exec awk {NR==${a}{print $1}} clk_gate_names]
}

Getting below error: awk: NR==${a}{print $1} awk: ^ syntax error

Please throw some light here. Thanks in advance :)

glenn jackman
  • 238,783
  • 38
  • 220
  • 352

3 Answers3

2

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:

  1. 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.
  2. 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…
Community
  • 1
  • 1
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
0

try below logic -

awk -v a=1 'NR==a {print $1}' f

As per your code -

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]
}

let me know if it satisfy your req. and share the output(desired output/error)

VIPIN KUMAR
  • 3,019
  • 1
  • 23
  • 34
  • Now i am getting below error % exec awk -v a="$a" 'NR==a{print $1}' clk_gate_name can't read "1": no such variable – argha chakraborty Feb 28 '17 at 06:28
  • Can you try below cmd, just for testing if variable is fetching value properly or not as i don't have any idea about what you are trying to achive in code - `awk -v a=1 'NR==a {print $1}'` clk_gate_names (in your code) – VIPIN KUMAR Feb 28 '17 at 07:49
0
awk -v a=${a} "NR==a{print \$1}" filename

Check :

http://phaseit.net/claird/comp.lang.tcl/fmm.html

AWK: who | awk '{ print $1 }' works from the command line, but my Tcl interpreter rejects exec who | awk '{ print $1 }' I often hear that from people who haven't yet learned to ask tclsh to interpret exec who | awk {{ print $1 }} Notice that the '-s aren't "about" awk; they're just conventional quoting in the shells from which one most often accesses awk. exec doesn't use the shells. {} perform an analogous function of quoting for Tcl. More examples appear in this Google thread. Alexandre Ferrieux gives a correct and concise explanation in another thread. The problem at hand for him happened to do with sed, but that's essentially inconsequential; the command-line syntaxes of awk and sed exhibit identical symptoms. Briefly, as the error messages which arise themselves say, the single quotes (') are the problem. These are shell markup for strings which shall not be substituted. /bin/sh and others remove this markup before calling awk.

In Tcl, in contrast, braces {} serve the same purpose. Braces have the advantage that they can be nested. One conclusion: brace the braces that awk expects: awk -F " " {{print $1}}. Note that, in this last, it is not necessary to escape the dollar-sign, because the outer brace protect it, too.

Much the same has been written several times by Richard Suchenwirth, Mark Janssen, Kevin Kenny, and others; it's unlikely any particular expression was invented in isolation.

P....
  • 17,421
  • 2
  • 32
  • 52