2
29/0.060

Is there any way to do above floating operation in shell script. I tried these

awk '{printf $1/0.060}' <<<29 It works fine,

awk '{printf $1/0.060}' <<<$test where test=29 also works fine.

But not

awk '{printf $1/$test2}' <<<29 where test2=0.060 Resulting 1, but answer is 483.333

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
Veerendra K
  • 271
  • 1
  • 3
  • 13

1 Answers1

2

The problem is that awk expands positional parameters from parsed input, but not shell variables. What you need therefore is:

awk '{printf $1/'$test2'}' <<<29

This allows the shell to expand $test2, but not $1.

AFH
  • 17,300
  • 3
  • 32
  • 48
  • `awk` also has the built-in `ENVIRON`, so this also works: `export test2=0.060; awk '{printf $1/ENVIRON["test2"]}' <<<29`. The `export` is needed to add `test2` to the environment so that `awk` can pick it up. – AFH Oct 19 '15 at 10:56