简体   繁体   English

awk或sed更改文件中的列值

[英]awk or sed to change column value in a file

I have a csv file with data as follows 我有一个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

I would like to shorten the value in the 5th column. 我想缩短第5列中的值。

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

这将四舍五入并在第一行打印.47而不是.46 ,但这也许是理想的。

Try with this: 试试这个:

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

So far, the output is at GNU/Linux standard output, so 到目前为止,输出为GNU / Linux标准输出,因此

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

will send the desired result to out_filename 会将所需结果发送到out_filename

If rounding is not desired, ie 0.466028518635 needs to be printed as 0.46 , use: 如果不需要舍入,即需要将0.466028518635打印为0.46 ,请使用:

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

(This can another example of Useless use of cat ) (这可能是猫无用的另一个例子)

You want it in perl, This is it: 您要在perl中使用它,就是这样:

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

tested below: 测试如下:

> 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
sed -r 's/^(([^,]+,){4}[^,]{4})[^,]*/\1/' file.csv

This might work for you (GNU sed): 这可能对您有用(GNU sed):

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

This replaces the 5th occurence of non-commas to no more than 4 characters length. 这会将第5次出现的非逗号替换为不超过4个字符的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM