Richard Rublev Richard Rublev - 1 year ago 94
Linux Question

How to divide with awk?

I want to read file and divide all elements by 0.03.Precision is IMPORTANT.Then to save result in output file.


var=$(cat 262_V01_C00_R000_TEx_BL_2048H.dat)
mapfile var < infile

awk '{for(i=1;i<=NF;i++) $i=$i*100/3}1' infile > output

But I got line 4: infile: No such file or directory

Sample input


Sample output


Answer Source

You can use this awk:

awk '$0+0 == $0 { printf "%.3f\n", $0 / .03 }' file
  • $0+0 == $0 will make sure to execute this division for lines with valid numbers only.
  • printf "%.3f" will print result with 3 precision points.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download