Xref: utzoo comp.unix.questions:25737 comp.unix.shell:287 Path: utzoo!attcan!uunet!motcid!ellis From: ellis@motcid.UUCP (John T Ellis) Newsgroups: comp.unix.questions,comp.unix.shell Subject: Re: Counting characters with unix utilities Message-ID: <4457@orchid12.UUCP> Date: 25 Sep 90 12:47:55 GMT References: <4002@umbc3.UMBC.EDU> Organization: Motorola Inc. - Cellular Infrastructure Div., Arlington Heights, IL 60004 Lines: 34 In article <4002@umbc3.UMBC.EDU> rouben@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