开发者

Bash: sort numbers with exponents

I was trying to sort one file w开发者_运维百科ith 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?


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

Sort numerically, using the standard C function strtod to 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'.


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


 

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


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

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):

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜