65

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.

Taryn
  • 242,637
  • 56
  • 362
  • 405
user1598776
  • 815
  • 1
  • 7
  • 7

3 Answers3

57

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
Alan Curry
  • 14,255
  • 3
  • 32
  • 33
20

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 Unix Sort with Tab Delimiter might be helpful.

Community
  • 1
  • 1
Levon
  • 138,105
  • 33
  • 200
  • 191
2

To list files based on size in asending order.

find ./ -size +1000M -exec ls -tlrh {} \; |awk -F" " '{print $5,$9}'  | sort -n\
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228