为什么AWK拒绝总结花车 [英] Why does AWK refuse to sum up floats
问题描述
我面临着 AWK
一个相当奇怪的问题,在这里我要计算列的平均值。这是测试输入表格我的文件:
I'm facing a rather strange problem withawk
where I want to calculate the average of a column. This is the test input form my file:
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 += $1} 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
我检查格式和输入文件等字符,但我不能让 AWK
来总结一下那些讨厌的浮动。
I've checked the formatting and characters of the input file etc. but I can't getawk
to sum up those pesky floats.
任何想法?
我在bash 3.2.48在OS X 10.8.5运行 AWK
20070501版本。
I'm running awk
version 20070501 in bash 3.2.48 on OS X 10.8.5.
由于@sudo_O正确推断,问题是我的语言环境。在文件中使用,
更换。
产生正确的结果。这显然不是我要找的,虽然如此,我需要做我的语言环境的东西这是目前设定到解决方案:
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:
$ 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=
我想保持的数字,货币和日期的语言环境,我认为。哪个区域,我需要改变(以及如何),使 AWK
工作?
推荐答案
这个问题不是 AWK
在这里。明确使用花车,看看你会得到什么:
The problem is not awk
here. Explicitly use floats and see what you get:
$ awk '{sum+=sprintf("%f",$1)}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 locale
command.
因此,使用你的 LC_NUMERIC
我可以重现你的结果:
So using your LC_NUMERIC
I can reproduce your results:
$ LC_NUMERIC="de_CH.UTF-8" awk '{sum += $1} END {print sum; print sum / NR}' file
3
0,375
解决方法是设置你的 LC_NUMERIC
或 LC_ALL
到 C
或其他任何使用
作为小数点分隔符:
The fix is to set your LC_NUMERIC
or LC_ALL
to C
or anything else that use .
as the decimal separator:
$ LC_NUMERIC="C" awk '{sum += $1} END {print sum; print sum / NR}' file
4.534
0.56675
请参阅男子区域
了解更多信息。
这篇关于为什么AWK拒绝总结花车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!