Sorting floats with exponents with 'sort -g

2019-04-27 09:44发布

I have a file with floats with exponents and I want to sort them. AFAIK 'sort -g' is what I need. But it seems like it sorts floats throwing away all the exponents. So the output looks like this (which is not what I wanted):

$ cat file.txt | sort -g
8.387280091e-05
8.391373668e-05
8.461754562e-07
8.547354437e-05
8.831553093e-06
8.936111118e-05
8.959458896e-07

This brings me to two questions:

  1. Why 'sort -g' doesn't work as I expect it to work?
  2. How cat I sort my file with using bash commands?

3条回答
相关推荐>>
2楼-- · 2019-04-27 10:19

Here's a neat trick:

$ sort -te -k2,2n -k1,1n test.txt 
8.461754562e-07
8.959458896e-07
8.831553093e-06
8.387280091e-05
8.391373668e-05
8.547354437e-05
8.936111118e-05

The -te divides your number into two fields by the e that separates out the mantissa from the exponent. the -k2,2 says to sort by exponent first, then the -k1,1 says to sort by your mantissa next.

Works with all versions of the sort command.

查看更多
倾城 Initia
3楼-- · 2019-04-27 10:21

Your method is absolutely correct

cat file.txt | sort -g

If the above code is not working , then try this

sed 's/\./0000000000000/g' file.txt | sort -g | sed 's/0000000000000/\./g'

Convert '.' to '0000000000000' , sort and again subsitute with '.'. I chose '0000000000000' to replace so as to avoid mismatching of the number with the inputs. You can manipulate the number by your own.

查看更多
甜甜的少女心
4楼-- · 2019-04-27 10:41

The problem is that in some countries local settings can mess this up by using , as the decimal separator instead of . on a system level. Check by typing locale in terminal. There should be an entry

LC_NUMERIC=en_US.UTF-8

If the value is anything else, change it to the above by editing the locale file

sudo gedit /etc/default/locale

That's it. You can also temporarily use this value by doing

LC_ALL=C sort -g file.dat

LC_ALL=C is shorter to write in terminal, but putting it in the locale file might not be preferable as it could alter some other system-wide behavior such as maybe time format.

查看更多
登录 后发表回答