Less expensive version of `sort -n | uniq -c | sort -n`

awkperformancesortuniq

I have an unsorted list of IPs that I need to count and sort by occurrences.
I use sort -n | uniq -c | sort -n and that works well, but I'd like something less expensive… surely awk can do this?

Input

1.1.1.1
2.2.2.2
1.1.1.1
3.3.3.3
2.2.2.2
1.1.1.1

Output

3 1.1.1.1
2 2.2.2.2
1 3.3.3.3

Best Answer

With single awk process:

awk '{ a[$1]++ }END{ for(i in a) print a[i],i }' file

The output:

3 1.1.1.1
2 2.2.2.2
1 3.3.3.3

To output records sorted by number of occurrences in descending order use the following GNU awk approach:

awk 'BEGIN{ PROCINFO["sorted_in"]="@val_num_desc" }{ a[$1]++ }
     END{ for(i in a) print a[i],i }' file
Related Question