bash 为什么AWK拒绝总结浮动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18946689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:38:59  来源:igfitidea点击:

Why does AWK refuse to sum up floats

bashawkfloating-point

提问by Max Leske

I'm facing a rather strange problem withawkwhere I want to calculate the average of a column. This is the test input form my file:

我在awk计算列的平均值时遇到了一个相当奇怪的问题。这是我的文件的测试输入:

1
2
0.4
0.250
0.225
0.221
0.220
0.218

And this is the script I'm trying to run:

这是我试图运行的脚本:

awk '{sum += } END {print sum; print sum / NR}' ~/Desktop/bar.txt

What I expect as output is:

我期望的输出是:

<calculated sum>
<calculated average>

But this is what I get invariably:

但这就是我总是得到的:

3
0,375

I've checked the formatting and characters of the input file etc. but I can't getawkto sum up those pesky floats.

我检查了输入文件的格式和字符等。但我无法awk总结那些讨厌的浮点数。

Any ideas?

有任何想法吗?

I'm running awkversion 20070501 in bash 3.2.48 on OS X 10.8.5.

awk在 OS X 10.8.5 上的 bash 3.2.48 中运行版本 20070501。

Update

更新

As @sudo_O correctly deduced, the problem is my locale. Replacing the .with a ,in the file yields the correct results. That's obviously not the solution I'm looking for though so I need to do something with my locale which is currently set to:

正如@sudo_O 正确推断的那样,问题是我的语言环境。.,文件中的a替换会产生正确的结果。这显然不是我正在寻找的解决方案,所以我需要对当前设置为的语言环境做一些事情:

$ locale
LANG="de_CH.UTF-8"
LC_COLLATE="de_CH.UTF-8"
LC_CTYPE="de_CH.UTF-8"
LC_MESSAGES="de_CH.UTF-8"
LC_MONETARY="de_CH.UTF-8"
LC_NUMERIC="de_CH.UTF-8"
LC_TIME="de_CH.UTF-8"
LC_ALL=

I'd like to keep numeric, monetary and date locales I think. Which locale do I need to change (and how), to make awkwork?

我想保留我认为的数字、货币和日期语言环境。我需要更改哪个语言环境(以及如何更改)才能awk工作?

回答by Chris Seymour

The problem is not awkhere. Explicitly use floats and see what you get:

问题不在awk这里。显式使用浮动,看看你得到了什么:

$ awk '{sum+=sprintf("%f",)}END{printf "%.6f\n%.6f\n",sum,sum/NR}' file
4.534000
0.566750

It looks like it's probably your locale as your output uses a ,as the decimal separator so post the output of the localecommand.

看起来它可能是您的语言环境,因为您的输出使用 a,作为小数点分隔符,因此请发布locale命令的输出。



So using your LC_NUMERICI can reproduce your results:

因此,使用您的LC_NUMERICI 可以重现您的结果:

$ LC_NUMERIC="de_CH.UTF-8" awk '{sum += } END {print sum; print sum / NR}' file
3
0,375

The fix is to set your LC_NUMERICor LC_ALLto Cor anything else that use .as the decimal separator:

解决方法是将您的LC_NUMERIC或设置LC_ALLC或其他任何.用作小数分隔符的内容:

$ LC_NUMERIC="C" awk '{sum += } END {print sum; print sum / NR}' file
4.534
0.56675

See man localefor more information.

请参阅man locale以获取更多信息。