Xref: utzoo comp.unix.questions:25703 comp.unix.shell:275 comp.lang.perl:2395 Path: utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!math.lsa.umich.edu!math.lsa.umich.edu!emv From: emv@math.lsa.umich.edu (Edward Vielmetti) Newsgroups: comp.unix.questions,comp.unix.shell,comp.lang.perl Subject: Re: Counting characters with unix utilities Message-ID: Date: 23 Sep 90 22:16:58 GMT References: <4002@umbc3.UMBC.EDU> Sender: usenet@math.lsa.umich.edu Organization: University of Michigan Math Dept., Ann Arbor MI. Lines: 25 In-Reply-To: rouben@math9.math.umbc.edu's message of 23 Sep 90 20:34:26 GMT 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 This is what I came up with in perl, after about 15 minutes of digging in the perl info pages: cat file | perl -ne '$c += tr/A/A/; if (eof()) {print "$c\n";}' Going back to the tr man page this one seems to work too: cat file | tr -cd 'A' | wc -c I don't see an easy perl equivalent of the "tr -cd" idiom. --Ed Edward Vielmetti, U of Michigan math dept moderator, comp.archives