bash Linux 排序不适用于负浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10031156/
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
Linux sort doesn't work with negative float numbers
提问by tsusanka
How to sort this kind of input?
如何对这种输入进行排序?
0.00159265291648695254
-0.00318530179313823899
0
0.00999983333416666468
0.00362937767285478371
0.00477794259012844049
-0.00637057126765263261
0.00681464007477014026
-0.00840724736714870645
-0.00522201549675090458
Either sort -n dataand sort -g dataprocudes this:
要么sort -n data并sort -g data产生这个:
0
0.00159265291648695254
-0.00318530179313823899
0.00362937767285478371
0.00477794259012844049
-0.00522201549675090458
-0.00637057126765263261
0.00681464007477014026
-0.00840724736714870645
0.00999983333416666468
On the other hand -1.whateverwould be in front of the zero. I need the sort to notice the minus signs. Thank you.
另一方面-1.whatever将在零之前。我需要排序来注意减号。谢谢你。
回答by tsusanka
All those troubles did my local settings. My ubuntu is in Czech:
所有这些麻烦都影响了我的本地设置。我的 ubuntu 是捷克语:
$ echo $LANG
cs_CZ.UTF-8
In this local setting it's not a decimal point, rather a decimal comma that seperates integer from the rest (as we were thought in math classes, in our language we really do write comma instead of a point).
在这个本地设置中,它不是一个小数点,而是一个将整数与其他整数分开的十进制逗号(正如我们在数学课上所认为的那样,在我们的语言中,我们确实写了逗号而不是点)。
Therefore:
所以:
echo '0,03 >> 0,4 >
> -0,3 >
> 0' | sort -n
> 0
> -0,3 >
> 0,4 >
0,03 >
If you are writing a bash script, set the sorting routine to use the "normal" settings.
如果您正在编写 bash 脚本,请将排序例程设置为使用“正常”设置。
export LC_ALL=C
回答by Diego Torres Milano
The problem may be in your sort command. If I run the same my result is as expected:
问题可能出在您的排序命令中。如果我运行相同的结果,则符合预期:
$ echo '0.00159265291648695254
> -0.00318530179313823899
> 0
> 0.00999983333416666468
> 0.00362937767285478371
> 0.00477794259012844049
> -0.00637057126765263261
> 0.00681464007477014026
> -0.00840724736714870645
> -0.00522201549675090458' | sort -n
-0.00840724736714870645
-0.00637057126765263261
-0.00522201549675090458
-0.00318530179313823899
0
0.00159265291648695254
0.00362937767285478371
0.00477794259012844049
0.00681464007477014026
0.00999983333416666468
You shoud use GNU sort if not using it
如果不使用它,你应该使用 GNU sort
sort (GNU coreutils) 5.93
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.

