How to count dup records in file?

Hi Gurus,

I need to count the duplicate records in file
file

abc
abc
def
ghi
ghi
jkl

I want to get below result:

abc ,2
abc, 2
def ,1
ghi ,2
ghi, 2
jkl ,1

or

abc ,2 
def ,1
ghi,2
jkl,1

Thanks in advance

cat <file> | uniq -c | sort -nr
1 Like

Your example file is sorted. If this is true, and you don't mind the output format

uniq -c file

awk can do the same and format the output

awk '(NR>1 && $0!=prev) {print prev,c; c=0} {c++; prev=$0} END {if (NR>1) print prev,c}' file

Instead of prev,c you can do prev", "c .

1 Like

try this

cat infile | awk " {rec[$1]=$1;c[$1]++} END { for (a in rec) print rec[a],c[a]} "