Counting characters with unix utilities

Dave Turner dmt at PacBell.COM
Wed Sep 26 07:01:20 AEST 1990


In article <4002 at umbc3.UMBC.EDU> rouben at math9.math.umbc.edu () writes:
>How can I count the number of occurrences of a given character in a file?
>It can be done rather trivially in C, but I wonder if it can also be done
>using standard unix utilities like awk, sed, tr, wc, etc.
>
>I feel that I am missing something, and that there should be an easy way
>to count characters a la unix.  Any hints?

The following will count all the occurrences of all character types in
a file. Simple modifications could limit it to those of interest.

cat file | sed -n -e "s/./&\\
/gp" | sort | uniq -c

Note: the first line of output is the number of newline characters.




-- 
Dave Turner	415/823-2001	{att,bellcore,sun,ames,decwrl}!pacbell!dmt



More information about the Comp.unix.shell mailing list