Ibrahim Quraish
Ibrahim Quraish

Reputation: 4099

How to correct this awk code without eval?

The following piece of awk code works fine if I use eval builtin

var='$1,$2'
ps | eval "awk '{print $var}'"

But when I try to knock off eval and use awk variable as substitute then I am not getting the expected result

ps | awk -v v1=$var '{print v1}'   # output is $1,$2
ps | awk -v v1=`echo $var` '{print v1}'  # output is same as above
ps | awk -v v1=$var '{print $v1}'  # output is all the fields of ps command
ps | eval "awk -v v1=$var '{print v1}'"  # output is column of comma

How to get the desire output without using eval?

Upvotes: 2

Views: 102

Answers (1)

devnull
devnull

Reputation: 123608

Use double-quotes in the awk command:

ps | awk "{print $var}"

Upvotes: 2

Related Questions