Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!wuarchive!udel!haven!uvaarpa!mmdf From: aks@hub.ucsb.edu (Alan Stebbens) Newsgroups: comp.lang.perl Subject: Re: Counting characters with Unix utilities Message-ID: <1990Sep24.184517.7963@uvaarpa.Virginia.EDU> Date: 24 Sep 90 18:45:17 GMT Sender: mmdf@uvaarpa.Virginia.EDU (Uvaarpa Mail System) Reply-To: aks@hub.ucsb.edu Organization: The Internet Lines: 15 In article <4002@umbc3.UMBC.EDU> rouben@math9.math.umbc.edu writes: rouben> How can I count the number of occurrences of a given character rouben> in a file? It can be done rather trivially in C, but I wonder rouben> if it can also be done using standard unix utilities like awk, rouben> sed, tr, wc, etc. rouben> The closest I have come to this is the following construction: rouben> cat file | tr -c 'A' '' | wc -c ed> Going back to the tr man page this one seems to work too: ed> cat file | tr -cd 'A' | wc -c ed> I don't see an easy perl equivalent of the "tr -cd" idiom. Try: perl -ne '$s+=tr/A//;if(eof){print $s,"\n";}' file