Passing a parameter to AWK

Hi All,

I am trying to pass a parameter to AWK on my KSH shell prompt as below.

var1=2
echo $var1
awk -v var2=${var1} '{print var2}' testfile.txt

I am passing the input file (testfile) to awk to get some o/p. It is having 10 records.

When I run AWK, it is throwing the following errors
awk: syntax error near line 1
awk: bailing out near line 1

I googed this forum, the syntax i am using is correct, but dont know why it is failing in this case.

My OS is SOLARIES,is there any difference syntax for AWK for passing parameters to AWK on sun solaries?

Can any one please help.

---------- Post updated at 12:46 AM ---------- Previous update was at 12:38 AM ----------

Hi All,

It got solved. The correct syntax is

awk '{print var2}' var2="$var1" test111.xls

I think there is syntax change in Sun solaries from BASH it seems.

In bash, generally we pass -v option before print, but in solaries, this is working if I use -v option after print.

Don't use awk with Solaris. /usr/bin/awk is a legacy binary which is there only to provide compatibility with ancient scripts that might still wander around.

Instead, use either nawk or /usr/xpg4/bin/awk.