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 :)

7
  • try exec awk -v a=${a} 'NR==a{print $1}' clk_gate_names Commented Feb 28, 2017 at 5:51
  • 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 Commented Feb 28, 2017 at 6:28
  • No it didnt work. What i tried is exec awk -v a=${a} {NR==a{print $1}} clk_gate_names is having no syntax error but not printing anuthing at same time. Can we on this think what is wrong here. :) Commented Feb 28, 2017 at 6:40
  • exec awk -v a=${a} {NR==a{print $1}} try this Commented Feb 28, 2017 at 6:44
  • % exec awk -v a=${a} 'NR==a{print \$1}' clk_gate_names awk: 'NR==a{print awk: ^ invalid char ''' in expression Commented Feb 28, 2017 at 6:47

3 Answers 3

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…
Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

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
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)
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.

Comments

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.