20

I tried to sort these number with Unix sort, but it doesn't seem to work:

    2e-13
    1e-91
    2e-13
    1e-104
    3e-19
    9e-99

This is my command:

sort -nr file.txt

What's the right way to do it?

neversaint
  • 60,904
  • 137
  • 310
  • 477
  • 3
    Use python, sorted, key = lambda x: float(x). Python can be used for one-liners. What version of Python is pre-installed? – Hamish Grubijan Apr 15 '10 at 02:19
  • @Hamish Grubijan - You probably should have posted that as an answer, +1. Its a lot more portable than relying on GNU extensions. – Tim Post Apr 15 '10 at 08:38

4 Answers4

41

Use -g (long form --general-numeric-sort) instead of -n. The -g option passes the numbers through strtod to obtain their value, and it will recognize this format.

I'm not sure if this is available on all implementations of sort, or just the GNU one.

Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166
  • 1
    I'm pretty sure it was initially GNU centric, but I can't think of a `sort` that doesn't offer it now. Might be problematic, but I think only on very old systems. – Tim Post Apr 15 '10 at 08:41
  • 2
    This did not sort 0 and 0.1 properly for me. Had to add LANG=C to my pipe `... | LANG=C sort -g` – user3132194 Apr 24 '18 at 07:18
8

if your sort doesn't have -g, another way.

$ printf "%.200f\n" $(<file) |sort -n |xargs printf "%g\n"
1e-104
9e-99
1e-91
3e-19
2e-13
2e-13

$ sort -g file
1e-104
9e-99
1e-91
3e-19
2e-13
2e-13

$ printf "%.200f\n" `cat file` |sort -n |xargs printf "%g\n"
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
7

Just do two things:

  1. Use -g (or --general-numeric-sort) to make sort treat Exp-numbers correctly.
  2. Use LC_ALL=C. The sort is very sensible to locale if your data may contain some language-specific symbols except ASCII. So using LC_ALL=C is the universal approach for every case you use the sort, it makes sort to treat the input stream as binary, and you wouldn't have any problems.

So the universal solution is:

cat file.txt | LC_ALL=C sort -gr | less

Also I made an alias for sort in my .bashrc file:

alias csort="LC_ALL=C sort"

for much convinient use.

Alex Medveshchek
  • 501
  • 4
  • 12
1

Ok, here is a not fully tested version of Python script. Supposed usage:

sort_script.py file.txt

Unfortunately I developed this on Windows, and with 2 different versions of Python installed I cannot test it properly. Warning: requires newest Python (with, print functions added or changed). Note: back_to_file flag can be an optional parameter, although with Unix you can always redirect ... even in Windows you can.

#!/usr/bin/env python3.1
# Note: requires newer python

import sys

#Remove this line:
sys.argv = ('', 'file.txt')

assert(len(sys.argv) == 2)

with open(sys.argv[1], 'r') as fin:
    lines = fin.readlines()

lines_sorted = sorted(lines, key=lambda x: float(x))

back_to_file = False # Change this if needed

if back_to_file:
    with open(sys.argv[1], 'w') as fout:
        fout.writelines(lines_sorted)
else:
    for lns in lines_sorted:
        print(lns, end='') # Suppress new line
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147