Sorting Scientific Number With Unix Sort

后端 未结 4 1759
面向向阳花
面向向阳花 2021-02-02 12:12

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

4条回答
  •  旧巷少年郎
    2021-02-02 12:35

    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.

提交回复
热议问题