There is a text file:
$ cat tempfile.txt
123
567
345
123
789
234
123
234
345
789
and my desired output is the following:
123,3
789,2
345,2
I need to sort each (1) in order of the number of occurrences, and (2) when the numbers of the occurrences are the same, the one with bigger numerical value should be ordered first, and (3) only top 3 are shown.
I tried this:
tr -c '[:alnum:]' '[\n*]' < tempfile.txt | sort -nr | uniq -c | sort -nr | head -3
But this only shows like this: I need to swap the position of the number of occurrences and digits, and separate those two by comma, and when the number of the occurrence is the same sort the bigger numerical digits first.
3 123
2 234
2 345