Bash: sort numbers with exponents

2019-03-17 20:49发布

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?

3条回答
来,给爷笑一个
2楼-- · 2019-03-17 21:27

 

perl -e 'print sort { $a<=>$b } <>' < input-file
查看更多
相关推荐>>
3楼-- · 2019-03-17 21:35

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

查看更多
别忘想泡老子
4楼-- · 2019-03-17 21:53

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'.

查看更多
登录 后发表回答