unix sort descending order

UnixSorting

Unix Problem Overview


I want to sort a tab limited file in descending order according to the 5th field of the records.

I tried

sort -r -k5n filename

But it didn't work.

Unix Solutions


Solution 1 - Unix

The presence of the n option attached to the -k5 causes the global -r option to be ignored for that field. You have to specify both n and r at the same level (globally or locally).

sort -t $'\t' -k5,5rn

or

sort -rn -t $'\t' -k5,5

Solution 2 - Unix

If you only want to sort only on the 5th field then use -k5,5.

Also, use the -t command line switch to specify the delimiter to tab. Try this:

sort  -k5,5 -r -n -t \t filename

or if the above doesn't work (with the tab) this:

sort  -k5,5 -r -n -t $'\t' filename

The man page for sort states:

> -t, --field-separator=SEP > use SEP instead of non-blank to blank transition

Finally, this SO question https://stackoverflow.com/questions/1037365/unix-sort-with-tab-delimiter might be helpful.

Solution 3 - Unix

To list files based on size in asending order.

find ./ -size +1000M -exec ls -tlrh {} \; |awk -F" " '{print $5,$9}'  | sort -n\

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1598776View Question on Stackoverflow
Solution 1 - UnixAlan CurryView Answer on Stackoverflow
Solution 2 - UnixLevonView Answer on Stackoverflow
Solution 3 - Unixuser2550384View Answer on Stackoverflow