Allow me to clarify:
Assume I have some keywords with frequency of their usage:
12 Hi
7 Hash
7 C++
9 Superuser
17 Stackoverflow
9 LaTeX
42 Life
9 Ubuntu
What I want, is to sort this data based on frequency in descending order and if there are some equal values, it should use the second column in ascending order.
sort -n -r foo.txt
Does the first part but then second column are also reversed
:
42 Life
17 Stackoverflow
12 Hi
9 Ubuntu
9 Superuser
9 LaTeX
7 Hash
7 C++
How can I achieve the following results?
42 Life
17 Stackoverflow
12 Hi
9 LaTeX
9 Superuser
9 Ubuntu
7 C++
7 Hash
I think I have to use -k
argument but I can't figure out how!
I want to know how this can be done using solely sort
command of bash
. However if it's not possible to achieve this only by sort
, other commands should be Bourne shell compatible.
Best Answer
Specify the sort keys separately with the criteria:
This specifies that the first key is sorted numerically in reverse order while the second is sorted as per the default sort order.
Quoting from POSIX sort:
This would produce: