![使用 awk 乘以一列](https://linux22.com/image/46829/%E4%BD%BF%E7%94%A8%20awk%20%E4%B9%98%E4%BB%A5%E4%B8%80%E5%88%97.png)
如何使用 ,将一列乘以一个整数(例如 3)并用新值替换旧值awk
?
输入:
数据 9390.900391 10573.089844 80.000000 200.000000 2.700000 数据 17762.810547 18536.189453 85.000000 200.000000 2.700000
预期输出:(将最后一列乘以 3 后)
数据 9390.900391 10573.089844 80.000000 200.000000 8.100000 数据 17762.810547 18536.189453 85.000000 200.000000 8.100000
答案1
尝试:
$ awk 'NF{$NF = sprintf("%.6f", $NF*3)}1' file
Data 9390.900391 10573.089844 80.000000 200.000000 8.100000
Data 17762.810547 18536.189453 85.000000 200.000000 8.100000
更改$NF
为您要更改的字段所在的位置$n
。n
答案2
使用for
循环:
awk '/^$/{next}; {for(i=1;i<=5;i++){printf "%s\t", $i}; printf "%.6f\n", $6*3}' file
Data 9390.900391 10573.089844 80.000000 200.000000 8.100000
Data 17762.810547 18536.189453 85.000000 200.000000 8.100000