Bash:用指数对数字进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4324923/
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-17 23:00:50  来源:igfitidea点击:

Bash: sort numbers with exponents

bashsortingcommand-line

提问by Fungsten

I was trying to sort one file with numeric values like this:

我试图用这样的数值对一个文件进行排序:

414e-05
435e-05
0.5361
0.7278
0.1341
0.9592
0.2664

With sort all the numers get sorted except for the ones with the exponent, is there some way for sort function to evaluate this expression?

使用 sort 对所有数字进行排序,除了那些带有指数的数字,排序函数是否有某种方法来评估这个表达式?

回答by unwind

If your version of the sort command is new enough, it should support the -goption (or --general-numeric-sort)if you like your options long). It is described like this in the info manual:

如果您的 sort 命令版本足够新,它应该支持该-g选项(或者--general-numeric-sort)如果您喜欢您的选项)。它在信息手册中是这样描述的:

Sort numerically, using the standard C function strtodto convert a prefix of each line to a double-precision floating point number. This allows floating point numbers to be specified in scientific notation, like '1.0e-34' and '10e100'.

数字排序,使用标准 C 函数strtod将每行的前缀转换为双精度浮点数。这允许以科学记数法指定浮点数,例如“1.0e-34”和“10e100”。

回答by dogbane

If you don't have sort -g, an alternative you can get is scisort.

如果您没有sort -g,您可以获得的替代方法是scisort

回答by mob

 

 

perl -e 'print sort { $a<=>$b } <>' < input-file

回答by Louis Gagnon

I don't have enough rep. to comment, so I am writing this to complement the accepted answer:

我没有足够的代表。发表评论,所以我写这篇文章是为了补充接受的答案:

for those who have locales which use a comma instead of a period to indicate decimals, the sorting of decimals will not work properly, as pointed out by HongboZhu

对于那些使用逗号而不是句点来表示小数的语言环境,小数的排序将无法正常工作,如 HongboZhu 所指出的

Solution: the sorting of lists with period-delimited numbers will work properly when using something like the following command (important is the LC_ALL=C):

解决方案:使用以下命令(重要的是 LC_ALL=C)时,以句点分隔的数字的列表排序将正常工作:

ls yourFolder|LC_ALL=C sort -g

This solution comes from the following post: https://unix.stackexchange.com/questions/506965/bash-sort-g-does-not-work-properly

此解决方案来自以下帖子:https: //unix.stackexchange.com/questions/506965/bash-sort-g-does-not-work-properly