[Solved] awk printf with variable


Note that foo=4/3 sets foo to the string 4/3. When that is printed via %f, ‘4/3’ is treated as 4; when that is printed with %s, it is printed as 4/3. If you want to evaluate the expression, you need it evaluated inside the script.

For example:

awk 'END {printf "%f\n", foonum/fooden }' foonum=4 fooden=3 /dev/null

Note that bash does not do floating point arithmetic. Thus this produces 1 as the output:

awk 'END {printf "%s\n", foo }' foo=$((4/3)) /dev/null

Maybe you want to use bc:

$ bc -l <<< "4/3"
1.33333333333333333333
$

0

solved awk printf with variable