Xref: utzoo comp.unix.questions:25751 comp.unix.shell:303 Path: utzoo!attcan!uunet!jarthur!elroy.jpl.nasa.gov!sdd.hp.com!zaphod.mps.ohio-state.edu!swrinde!ucsd!pacbell.com!pacbell!ptsfa!dmt From: dmt@PacBell.COM (Dave Turner) Newsgroups: comp.unix.questions,comp.unix.shell Subject: Re: Counting characters with unix utilities Message-ID: <5416@ptsfa.PacBell.COM> Date: 25 Sep 90 21:01:20 GMT References: <4002@umbc3.UMBC.EDU> Reply-To: dmt@PacBell.COM (Dave Turner) Followup-To: comp.unix.questions Organization: Pacific * Bell, San Ramon, CA Lines: 21 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. > >I feel that I am missing something, and that there should be an easy way >to count characters a la unix. Any hints? The following will count all the occurrences of all character types in a file. Simple modifications could limit it to those of interest. cat file | sed -n -e "s/./&\\ /gp" | sort | uniq -c Note: the first line of output is the number of newline characters. -- Dave Turner 415/823-2001 {att,bellcore,sun,ames,decwrl}!pacbell!dmt