reformating a text file [closed]

ぃ、小莉子 提交于 2020-01-03 04:57:07

问题


I've looked for existing answers, but am at a loss.

I need to take a plain text file:

1 - round decimals to tenths

2 - last group of numbers need to be four characters long, using zero to fill

3 - remove periods - found sed solution

4 - remove spaces - found sed solution

here is an example of the file I'm working with

5.41   3.08  795
4.82   2.69   19
3.60   2.83    8
4.24   3.10 1559

needs to be changed to:

54310795
48270019
36280008
42311559

Thanks - Andy


回答1:


you can do it in one-shot with awk:

 awk '{$0=sprintf("%.1f%.1f%04d", $1,$2,$3);gsub(/\./,"")}1' file 

test:

kent$  cat file
5.41   3.08  795
4.82   2.69   19
3.60   2.83    8
4.24   3.10 1559

kent$  awk '{$0=sprintf("%.1f%.1f%04d", $1,$2,$3);gsub(/\./,"")}1' file
54310795
48270019
36280008
42311559


来源:https://stackoverflow.com/questions/16046624/reformating-a-text-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!