如何用awk进行划分?

Ric*_*lev 3 linux bash awk

我想读取文件并将所有元素除以 0.03.Precision 很重要。然后将结果保存在输出文件中。

 #!/bin/bash

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
Run Code Online (Sandbox Code Playgroud)

但我得到了

a4.sh: line 4: infile: No such file or directory
Run Code Online (Sandbox Code Playgroud)

样本输入

-9.341203692800e+02
-9.320539972800e+02
-9.302205617600e+02
Run Code Online (Sandbox Code Playgroud)

样本输出

-31137.345
-31068.466
-31007.352
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 6

你可以使用这个awk:

awk '$0+0 == $0 { printf "%.3f\n", $0 / .03 }' file
-31137.346
-31068.467
-31007.352
Run Code Online (Sandbox Code Playgroud)
  • $0+0 == $0 将确保仅对具有有效数字的行执行此除法。
  • printf "%.3f" 将打印 3 个精度点的结果。