62

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
  • 234,956
  • 54
  • 359
  • 399
user1598776
  • 685
  • 1
  • 6
  • 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
  • 13,505
  • 3
  • 30
  • 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
  • 129,246
  • 33
  • 194
  • 186
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
  • 61,565
  • 12
  • 180
  • 225