Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting floats with exponents with 'sort -g' bash command

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?
like image 255
Alex Avatar asked Apr 25 '12 07:04

Alex


3 Answers

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.

like image 127
David W. Avatar answered Nov 06 '22 06:11

David W.


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.

like image 26
Jonatan Öström Avatar answered Nov 06 '22 07:11

Jonatan Öström


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.

like image 4
Debaditya Avatar answered Nov 06 '22 08:11

Debaditya