counting duplicates in a sorted sequence using command line tools counting duplicates in a sorted sequence using command line tools bash bash

counting duplicates in a sorted sequence using command line tools


how about;

$ echo "100 100 100 99 99 26 25 24 24" \    | tr " " "\n" \    | sort \    | uniq -c \    | sort -k2nr \    | awk '{printf("%s\t%s\n",$2,$1)}END{print}'

The result is :

100 399  226  125  124  2


uniq -c works for GNU uniq 8.23 at least, and does exactly what you want (assuming sorted input).


if order is not important

# echo "100 100 100 99 99 26 25 24 24" | awk '{for(i=1;i<=NF;i++)a[$i]++}END{for(o in a) printf "%s %s ",o,a[o]}'26 1 100 3 99 2 24 2 25 1