AWK或sed的一个文件来更改列值 [英] awk or sed to change column value in a file
本文介绍了AWK或sed的一个文件来更改列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数据的csv文件如下:
16:47:07,3,R-4-VM,230000000.,0.466028518635,131072,0,0,0,60,0
16:47:11.3,R-4-VM,2.5亿,0.50822578824,131072,0,0,0,0,0
16:47:14.3,R-4-VM,240000000.,0.488406067907,131072,0,0,32768,0,0
16:47:17.3,R-4-VM,230000000.,0.467893525702,131072,0,0,0,0,0
我想缩短在第5列的值。
所需的输出
16:47:07,3,R-4-VM,230000000.,0.46,131072,0,0,0,60,0
16:47:11.3,R-4-VM,2.5亿,0.50,131072,0,0,0,0,0
16:47:14.3,R-4-VM,240000000.,0.48,131072,0,0,32768,0,0
16:47:17.3,R-4-VM,230000000.,0.46,131072,0,0,0,0,0
您的帮助是非常AP preciated
解决方案
的awk'{$ 5 =的sprintf(%0.2克,$ 5)} 1'OFS =,FS =,输入
这将全面和打印 0.47
而不是 0.46
在第一行,但也许这是可取的
I have a csv file with data as follows
16:47:07,3,r-4-VM,230000000.,0.466028518635,131072,0,0,0,60,0
16:47:11,3,r-4-VM,250000000.,0.50822578824,131072,0,0,0,0,0
16:47:14,3,r-4-VM,240000000.,0.488406067907,131072,0,0,32768,0,0
16:47:17,3,r-4-VM,230000000.,0.467893525702,131072,0,0,0,0,0
I would like to shorten the value in the 5th column.
Desired output
16:47:07,3,r-4-VM,230000000.,0.46,131072,0,0,0,60,0
16:47:11,3,r-4-VM,250000000.,0.50,131072,0,0,0,0,0
16:47:14,3,r-4-VM,240000000.,0.48,131072,0,0,32768,0,0
16:47:17,3,r-4-VM,230000000.,0.46,131072,0,0,0,0,0
Your help is highly appreciated
解决方案
awk '{$5=sprintf( "%.2g", $5)} 1' OFS=, FS=, input
This will round and print .47
instead of .46
on the first line, but perhaps that is desirable.
这篇关于AWK或sed的一个文件来更改列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文