Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting Scientific Number With Unix Sort

Tags:

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?

like image 454
neversaint Avatar asked Apr 15 '10 02:04

neversaint


People also ask

How do you sort numbers in Unix?

-k Option: Unix provides the feature of sorting a table on the basis of any column number by using -k option. Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column.

How do I sort a text file numerically in Linux?

Sort a File Numerically To sort a file containing numeric data, use the -n flag with the command. By default, sort will arrange the data in ascending order. If you want to sort in descending order, reverse the arrangement using the -r option along with the -n flag in the command.

How do I sort in ascending order in Unix?

Option -n In Unix, when you try to sort a file in a numeric way, you can use the option '-n' with the sort command. This command is used to sort the numeric contents present in the file. Be default, it sorts in ascending order.

How does sort command work in Unix?

The sort command sorts the contents of a file, in numeric or alphabetic order, and prints the results to standard output (usually the terminal screen). The original file is unaffected. The output of the sort command will then be stored in a file named newfilename in the current directory.


2 Answers

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.

like image 149
Tyler McHenry Avatar answered Oct 23 '22 15:10

Tyler McHenry


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"
like image 38
ghostdog74 Avatar answered Oct 23 '22 14:10

ghostdog74