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:
- Why 'sort -g' doesn't work as I expect it to work?
- How cat I sort my file with using bash commands?
Here's a neat trick:
The
-te
divides your number into two fields by thee
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.Your method is absolutely correct
If the above code is not working , then try this
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.
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 typinglocale
in terminal. There should be an entryIf the value is anything else, change it to the above by editing the locale file
That's it. You can also temporarily use this value by doing
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.