Counting characters with unix utilities
John T Ellis
ellis at motcid.UUCP
Tue Sep 25 22:47:55 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.
>
>The closest I have come to this is the following construction:
>
>cat file | tr -c 'A' '' | wc -c
>
>which attempts to count the number of occurrences of the character "A"
>in the file. The "tr" command replaced all characters different from
>"A" by the null character, then "wc" counts all characterters in its input
>(unfortunately) also counting the null characters :-(
[Text Deleted]
Try the following:
cat file | tr -cs A-Za-z A-Za-z'\012' | sort | unique -c
If I understand tr, which is not necessarily true at this ungodly hour
of the morning :-), this should take the first occurence of either
A-Z or a-z and map it to A-Z or a-z with a line feed. Hence, you get
a long list with single characters. Sort it and push it through
the unique filter which with the -c option tells you the number
of times a character appeared.
Note: This will differentiate between A and a.
John
--
---------------------------------------------------+----------------------------
Any sufficiently advanced technology | John T. Ellis 708-632-7857
is indistinguishable from magic. :-} | Motorola Cellular
| ...uunet!motcid!ellis
More information about the Comp.unix.questions
mailing list