Xref: utzoo comp.unix.questions:25702 comp.unix.shell:274 Path: utzoo!attcan!uunet!aplcen!haven!umbc3!math9.math.umbc.edu!rouben From: rouben@math9.math.umbc.edu Newsgroups: comp.unix.questions,comp.unix.shell Subject: Counting characters with unix utilities Message-ID: <4002@umbc3.UMBC.EDU> Date: 23 Sep 90 20:34:26 GMT Sender: newspost@umbc3.UMBC.EDU Reply-To: rouben@math9.math.umbc.edu () Organization: Mathematics Department University of Maryland, Baltimore County Lines: 23 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 :-( I feel that I am missing something, and that there should be an easy way to count characters a la unix. Any hints? [If it matters, the operating system is ultrix and the shells are sh and csh.] -- Rouben Rostamian Telephone: (301) 455-2458 Department of Mathematics and Statistics e-mail: University of Maryland Baltimore County rostamian@umbc.bitnet Baltimore, MD 21228, U.S.A. rostamian@umbc3.umbc.edu