I need to sort a file by multiple columns. There are 12 columns and I need to sort first by column 2, then 11, and lastly 3. The third column should be in reverse order (highest to lowest values). I also want to save the sorted columns to a new document. This is the command I'm using but it's not working:

sort -k 2,2 Filename | sort -k 11,11 | sort -k 3,3 -r -o test_sorting.txt

What should I do differently?

  • 2
    Have you tried just using sort once? Sort -k ... -k ... etc. Otherwise it's ignoring any previous sorting earlier in the pipe, maybe
    – Guy
    Dec 21, 2016 at 21:58

1 Answer 1


You can chain keys in the same command, it will sort in the order specified.

sort -o test_sorting.txt -k 2,2 -k 11,11 -k 3,3r Filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.