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?
-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.
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.
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.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With