0

I need to sort a table based on two columns, one column is numerical and while other is string. I need to have descending order for numerical while I would like to have alphabetical for string. Use of '-r' option for sort does work for numerical but it gets applied for sting too. I wonder how to apply reverse option only for one column and not the other.

Cmd: sort -r -k5 -k3 -k1

Data: Input

 il ||     2 |      3 ||
 we ||     2 |      2 ||
 dt ||     0 |      2 ||
 di ||     0 |      2 ||
 cs ||    16 |      1 ||
 fd ||     5 |      1 ||
 df ||    14 |      0 ||
 np ||     9 |      0 ||
 dt ||     9 |      0 ||
 ta ||     0 |      0 ||
 rt ||     0 |      0 ||
 ps ||     0 |      0 ||

Expected

 il ||     2 |      3 ||
 we ||     2 |      2 ||
 di ||     0 |      2 ||
 dt ||     0 |      2 ||
 cs ||    16 |      1 ||
 fd ||     5 |      1 ||
 df ||    14 |      0 ||
 dt ||     9 |      0 ||
 np ||     9 |      0 ||
 ps ||     0 |      0 ||
 rt ||     0 |      0 ||
 ta ||     0 |      0 ||
1
  • Look at the flags for the -k option. Any global ordering option can also be specified for an individual key. Commented Oct 12, 2017 at 4:19

1 Answer 1

1

This works for me

$ sort -rk5 -k3 -k1b input

Output:

 il ||     2 |      3 ||
 we ||     2 |      2 ||
 di ||     0 |      2 ||
 dt ||     0 |      2 ||
 cs ||    16 |      1 ||
 fd ||     5 |      1 ||
 df ||    14 |      0 ||
 dt ||     9 |      0 ||
 np ||     9 |      0 ||
 ps ||     0 |      0 ||
 rt ||     0 |      0 ||
 ta ||     0 |      0 ||
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.