Code:
awk '{ a[$0]++ } END{ for (i in a) { print i ": " a[i] } }' infile.txt | sort
awk 'BEGIN{ PROCINFO["sorted_in"]="@ind_str_asc" } { a[$0]++ } END{ for (i in a) { print i ": " a[i] } }' infile.txt
This uses awk's associative arrays. Each array index is the line itself, and the value increments once each time the same line is encountered. Then at the end we just loop through the array and print the values.
In the first version, we pipe it through
sort afterwards in order to get sorted output. If you're using
gawk v.4+ you can use the second variation, as it has
array sorting built in.