awk not rounding with OFMT and $0

北城以北 提交于 2019-11-27 22:41:35

问题


I'm printing an array with 100 columns and I would like all columns to have 2 decimals. I would like to use print $0 and not have to individually specify the format for all columns.

OFMT does seen to work with $0:

echo '0.77767686 0.76555555 0.6667667 0.77878878' |awk '{CONVFMT="%.2g";OFMT="%.2g";print ($0+0);print ($0+0)"";print $0}' 

Results:

0.78
0.78
0.77767686 0.76555555 0.6667667 0.77878878

回答1:


Note that all input is treated as strings until implicitly converted by how it is used.

OFMT is used when strings are converted to numbers, e.g.:

<<< 0.77767686 awk '{ print 0+$0 }' OFMT='%.2g'

CONVFMT is used when numbers are converted into strings, e.g.:

<<< 0.77767686 awk '{ print "" 0+$0 }' CONVFMT='%.2g'

Output in both cases:

0.78

The latter converts $0 into a number and then concatenates it with the empty string.

To achieve this for every column I would suggest using a sensible setting of the input and output record separators:

<<< '0.77767686 0.76555555 0.6667667 0.77878878' \
awk '{ print 0+$0 RT }' CONVFMT='%.2g' RS='[ \t\n]+' ORS=''

Note the two conversions, first to a number with 0+$0 then back to a string by concatenating it with RT. RT will be set to the matched record separator. Note that this is GNU awk specific, for a more portable solution, use a loop, e.g.:

<<< '0.77767686 0.76555555 0.6667667 0.77878878' \
awk '{ for (i=1; i<=NF; i++) $i+=0 } 1' CONVFMT='%.2g'

Output in both cases:

0.78 0.77 0.67 0.78



回答2:


Why don't you use a for loop?

echo '0.77767686 0.76555555 0.6667667 0.77878878' |awk '{ for (i=1; i<=NF; i++) printf "%.2f\n", $i }'

Results:

0.78
0.77
0.67
0.78



回答3:


As others have mentioned you need to treat the field as a number to get a conversion. To combine some other ideas you can try:

awk '{ for (i=1; i<=NF; i++) $i += 0; print }'

That will convert every field to a number. You can just convert individual fields with $7 += 0 and so on. You could get fancier by using if (match($i, ...)) with some regexp to select only the numbers you want to convert.



来源:https://stackoverflow.com/questions/12873095/awk-not-rounding-with-ofmt-and-0

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