awk减少单个多个输入值的小数 [英] awk reduce decimals for a single multiple input values

查看:79
本文介绍了awk减少单个多个输入值的小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要转换以下内容

82.77, 69.30, 43.75, 33.44, 26.88, 26.83, 24.89, 24.88, 24.74, 23.07, 19.31

进入

82.8, 69.3, 44.0, 33.4, 26.8, ...

我认为awk似乎是执行此操作的简便方法.以下确实适用于单个输入值

I think awk seems to be the easy way to do this. The following does work for a single input value

echo 123.45567 | awk '{printf("%.1f\n", $1)}'

如何使用带有awk的空格分隔符对上述多个输入值执行此操作,或者有更好的方法吗?昨天我尝试使用

How can I do this for the above multiple input values with the space delimiter with awk or is there any better way to do this? Yesterday I was trying with java script which didn't work.

推荐答案

只需遍历每个字段:

$ awk  -F', ' '{for(i=1;i<=NF;i++)printf "%.1f%s",$i,(i==NF?RS:FS)}'

通过将字段分隔符设置为, (逗号后跟空格),输入格式将保留在输出中.条件(i==NF?RS:FS)检查我们是否到达记录中的最后一个字段,并相应地分离输出.

By setting the field separator to be , (a comma followed by a space) the input format is preserved in the output. The condition (i==NF?RS:FS) checks if we have reached the last field in the record and separates the output accordingly.

这篇关于awk减少单个多个输入值的小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆