我有一个文本文件,其中第四列包含许多小数位的数字。我需要将位数减少到0.1个小数位。

数据:

2015041416 KBOS TMP2M 62.034
2015041417 KBOS TMP2M 65.0826
2015041418 KBOS TMP2M 66.7278
2015041419 KBOS TMP2M 67.5033
2015041420 KBOS TMP2M 67.1731
2015041421 KBOS TMP2M 65.9252
2015041422 KBOS TMP2M 62.7644
2015041423 KBOS TMP2M 60.8136
2015041500 KBOS TMP2M 56.167
2015041501 KBOS TMP2M 55.7116
2015041502 KBOS TMP2M 51.8512
2015041503 KBOS TMP2M 51.6005
2015041504 KBOS TMP2M 50.1643
2015041505 KBOS TMP2M 49.2167
2015041506 KBOS TMP2M 48.273
2015041507 KBOS TMP2M 46.6407

最佳答案

awk '{printf "%s %s %s %.1f\n",$1, $2, $3, $4}' file > file2

输出到文件2:
2015041416 KBOS TMP2M 62.0
2015041417 KBOS TMP2M 65.1
2015041418 KBOS TMP2M 66.7
2015041419 KBOS TMP2M 67.5
2015041420 KBOS TMP2M 67.2
2015041421 KBOS TMP2M 65.9
2015041422 KBOS TMP2M 62.8
2015041423 KBOS TMP2M 60.8
2015041500 KBOS TMP2M 56.2
2015041501 KBOS TMP2M 55.7
2015041502 KBOS TMP2M 51.9
2015041503 KBOS TMP2M 51.6
2015041504 KBOS TMP2M 50.2
2015041505 KBOS TMP2M 49.2
2015041506 KBOS TMP2M 48.3
2015041507 KBOS TMP2M 46.6

Same output with bash and without awk:

printf "%s %s %s %.1f\n" $(< file) > file2

关于linux - 减少文本文件中第四列的小数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29634017/

10-13 03:58