I have a file like this:
A 0.77
C 0.98
B 0.77
Z 0.77
G 0.65
I want to sort the file numerically in descending order. I used this code:
sort -gr -k2,2 file.txt
I obtain this:
C 0.98
Z 0.77
B 0.77
A 0.77
G 0.65
In my real file I have several columns with the same number and they are ordered alphabetically. What I want is to sort numerically but not alphabetically when the numbers are equal, I want to obtain those columns unsorted alphabetically:
C 0.98
B 0.77
Z 0.77
A 0.77
G 0.65
But any random order is fine.
You can use this
sort
:There are 2 sort options used:
-k2rn
: First sort key is column 2; numerical, reverse-k1R
: Second sort key is column 1; randomOne in GNU awk that preserves the order of the first field (random in, equally random out):
Testing reverse order: