# How to correct this awk code without eval?

**URL:** <https://community.unix.com/t/how-to-correct-this-awk-code-without-eval/336747>\
**Category:** Shell Programming and Scripting\
**Created:** [November 18, 2013, 2:29am UTC](https://community.unix.com/t/how-to-correct-this-awk-code-without-eval/336747 "2013-11-18T02:29:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![royalibrahim](https://community.unix.com/letter_avatar/royalibrahim/32/5_5575768a8748004e209b776fc1b2916d.png) [@royalibrahim](https://community.unix.com/u/royalibrahim)\
**Post date:** [November 18, 2013, 2:29am UTC](https://community.unix.com/t/how-to-correct-this-awk-code-without-eval/336747/1 "2013-11-18T02:29:30Z")

</div>

Hi everyone,

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

```nohighlight
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

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [November 18, 2013, 3:17am UTC](https://community.unix.com/t/how-to-correct-this-awk-code-without-eval/336747/2 "2013-11-18T03:17:36Z")

</div>

The shell substitutes variables in "quotes".

```nohighlight
var="$1,$2"

```

But here you maybe want

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

```

?

---

<div class="post-metadata">

**Author:** ![royalibrahim](https://community.unix.com/letter_avatar/royalibrahim/32/5_5575768a8748004e209b776fc1b2916d.png) [@royalibrahim](https://community.unix.com/u/royalibrahim)\
**Post date:** [November 18, 2013, 4:52am UTC](https://community.unix.com/t/how-to-correct-this-awk-code-without-eval/336747/3 "2013-11-18T04:52:37Z")

</div>

> [@madeingermany](#):
>
> ```plaintext
> var='$1,$2'
> ps | awk '{print '"$var"'}'
> 
> ```
> 
> ?

Brilliant [MadeInGermany](http://www.unix.com/member.php?u=302116191)!! Also, could you please help me to how to use awk variable in this scenario?

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/5_5575768a8748004e209b776fc1b2916d.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [November 18, 2013, 5:07am UTC](https://community.unix.com/t/how-to-correct-this-awk-code-without-eval/336747/4 "2013-11-18T05:07:58Z")

</div>

The `$1,$2` is a piece of awk code.  
You cannot run a string (as assigned to an awk variable) as awk code.  
(Unless there were an `eval` built into awk.)  
This following awk code interprets a given string as you intend:

```nohighlight
var='1,2'
ps | awk -v v1="$var" 'BEGIN{n=split(v1,a,",")} {for(i=1;i<=n;i++) printf "%s\t",$(a); print ""}'

```

---

<div class="post-metadata">

**Author:** ![Jotne](https://community.unix.com/user_avatar/community.unix.com/jotne/32/2148_2.png) [@Jotne](https://community.unix.com/u/Jotne)\
**Post date:** [November 18, 2013, 5:28am UTC](https://community.unix.com/t/how-to-correct-this-awk-code-without-eval/336747/5 "2013-11-18T05:28:48Z")

</div>

This works.  
`ps | awk "{print $var}"`

Single quote does not expand variable.
