I have a data file as follows;
ALIC 5.55 2.65 3.45
ALIC 2.56 2.78 3.99
AMC4 3.55 3.66 4.88
AMC4 3.00 3.67 4.78
ALIC 1.00 1.00 1.00
ALIC 0.95 1.00 1.00
AMC4 0.50 1.00 1.00
AMC4 0.01 1.00 1.00I need to sort these lines w.r.t strings in the first column as follows;
ALIC 5.55 2.65 3.45
ALIC 2.56 2.78 3.99
ALIC 1.00 1.00 1.00
ALIC 0.95 1.00 1.00
AMC4 3.55 3.66 4.88
AMC4 3.00 3.67 4.78
AMC4 0.50 1.00 1.00
AMC4 0.01 1.00 1.00But when I execute sort -k 1 data, I see that numeric values in the second column are sorted as ascending order. Which command produce the above sorting?
2 Answers
There are two things you need to do.
First, sort -k 1 starts at key 1, but by default stops at the end of the line: to sort only on key 1, you need -k 1,1
Second, by default GNU sort performs a final "last resort" sort on the whole line - to suppress that, you need to add the --stable option.
So
$ sort --stable -k 1,1 data
ALIC 5.55 2.65 3.45
ALIC 2.56 2.78 3.99
ALIC 1.00 1.00 1.00
ALIC 0.95 1.00 1.00
AMC4 3.55 3.66 4.88
AMC4 3.00 3.67 4.78
AMC4 0.50 1.00 1.00
AMC4 0.01 1.00 1.00 This will sort by the first column in ascending order (as usual) and then by the second column in descending (reverse) order. The second column is interpreted as a number:
$ sort -k 1,1 -k 2nr,2 < in.txt
ALIC 5.55 2.65 3.45
ALIC 2.56 2.78 3.99
ALIC 1.00 1.00 1.00
ALIC 0.95 1.00 1.00
AMC4 3.55 3.66 4.88
AMC4 3.00 3.67 4.78
AMC4 0.50 1.00 1.00
AMC4 0.01 1.00 1.00You may want to add the --debug flag to see exactly on which keys sort operates:
$ sort --debug -k 1,1 -k 2nr,2 < in.txt
ALIC 5.55 2.65 3.45
____ ____
___________________
ALIC 2.56 2.78 3.99
____ ____
___________________
ALIC 1.00 1.00 1.00
____ ____
___________________
...