Help me to perform count & group by operation in shell scripting?

Hi All,

I want to display the distinct values in the file and for each distinct value how may occurance or there.

Test data:

test1.dat

20121105
20121105
20121105
20121105
20121106
20121106
20121106
20121105

I need to display the output like

Output

20121105  5
20121106  3

Please help me how to write the shell script for this.

Thanks in advance.

Thanks,
Chandu.

sort infile | uniq -c
5 20121105
3 20121106
1 Like

try also:

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

---------- Post updated at 02:21 PM ---------- Previous update was at 02:10 PM ----------

or for a shell example:

#!/bin/sh
 
while read var
do
  ar[$var]=$var
  (( arr[$var]++ ))
done < file
 
for v in ${ar[@]}
do
  echo $v ${arr[$v]}
done
1 Like