AWK或sed的在文件中改变列值(awk or sed to change column value

2019-09-02 07:54发布

我有一个数据csv文件如下

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

我想缩短在第5列的值。

所需的输出

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

你的帮助是高度赞赏

Answer 1:

awk '{$5=sprintf( "%.2g", $5)} 1' OFS=, FS=, input

这将圆化打印.47 ,而不是.46的第一线,但也许这是可取的。



Answer 2:

试试这个:

cat filename | sed 's/\(^.*\)\(0\.[0-9][0-9]\)[0-9]*\(,.*\)/\1\2\3/g'

到目前为止,输出为GNU / Linux的标准输出,所以

cat filename | sed 's/\(^.*\)\(0\.[0-9][0-9]\)[0-9]*\(,.*\)/\1\2\3/g' > out_filename

将发送所需的结果out_filename



Answer 3:

如果舍入是不期望的,即, 0.466028518635需要被打印为0.46 ,使用:

cat <input> | awk -F, '{$5=sprintf( "%.4s", $5)} 1' OFS=,

(这可以的另一示例无用使用猫 )



Answer 4:

你想在Perl中,这是它:

perl -F, -lane '$F[4]=~s/^(\d+\...).*/$1/g;print join ",",@F' your_file

下面的测试:

> cat temp
16:47:07,3,r-4-VM,230000000.,0.466028518635,131072,0,0,0,60,0
16:47:11,3,r-4-VM,250000000.,10.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
> perl -F, -lane '$F[4]=~s/^(\d+\...).*/$1/g;print join ",",@F' temp
16:47:07,3,r-4-VM,230000000.,0.46,131072,0,0,0,60,0
16:47:11,3,r-4-VM,250000000.,10.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


Answer 5:

sed -r 's/^(([^,]+,){4}[^,]{4})[^,]*/\1/' file.csv


Answer 6:

这可能会为你工作(GNU SED):

sed -r 's/([^,]{,4})[^,]*/\1/5' file

这取代非逗号的不超过4个字符长度的第五occurence。



文章来源: awk or sed to change column value in a file