linux "sort - Reverse sort order of a multicolumn file in BASH




2 Answers

sort -nrk 2,2

does the trick.

n for numeric sorting, r for reverse order and k 2,2 for the second column.

-k" option

I've the following file:

1 2 3
1 4 5
1 6 7
2 3 5
5 2 1

and I want that the file be sorted for the second column but from the largest number (in this case 6) to the smallest. I've tried with

sort +1 -2 file.dat

but it gives me the inversed order.

The results should be:

1 6 7
1 4 5
2 3 5
5 2 1
1 2 3



As mention most version of sort have the -r option if yours doesn't try tac:

$ sort -nk 2,2 file.dat | tac 
1 6 7
1 4 5
2 3 5
5 2 1
1 2 3

$ sort -nrk 2,2 file.dat 
1 6 7
1 4 5
2 3 5
5 2 1
1 2 3

tac - concatenate and print files in reverse




Related

linux bash unix sorting awk