Sorting in bash

Viewed 71403

I have been trying to get the unique values in each column of a tab delimited file in bash. So, I used the following command.

cut -f <column_number> <filename> | sort | uniq -c

It works fine and I can get the unique values in a column and its count like

105 Linux
55  MacOS
500 Windows

What I want to do is instead of sorting by the column value names (which in this example are OS names) I want to sort them by count and possibly have the count in the second column in this output format. So It will have to look like:

Windows 500
MacOS   105
Linux   55

How do I do this?

3 Answers

Using sed based on Tagged RE:

cut -f <column_number> <filename> | sort | uniq -c | sort -r -k1 -n | sed 's/\([0-9]*\)[ ]*\(.*\)/\2 \1/'

Doesn't produce output in a neat format though.

Related