Xref: utzoo comp.unix.questions:25811 comp.unix.shell:326 Path: utzoo!attcan!uunet!munnari.oz.au!metro!wolfen!hls0!george From: george@hls0.hls.oz (George Turczynski) Newsgroups: comp.unix.questions,comp.unix.shell Subject: Re: Counting characters with unix utilities Message-ID: <939@hls0.hls.oz> Date: 26 Sep 90 23:22:56 GMT References: <4002@umbc3.UMBC.EDU> Lines: 37 In article <4002@umbc3.UMBC.EDU>, rouben@math9.math.umbc.edu writes: [...Deleted...] > 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. [...Deleted...] OK, try this: awk -F'A' '{ sum+= (NF-1) } END { print sum }' file The single quotes around the "A" here are only to point out the "A", and aren't really necessary. It simply makes "A" the field separator and adds the number of fields (NF) less one to the total. You will see why the "-1" is necessary if you think about it. On some systems you may have to initialize sum to zero, with:- BEGIN { sum= 0 } > [If it matters, the operating system is ultrix and the shells are sh and csh.] Just for interest's sake, this is under SunOS 4.0.3. I trust this is what you were looking for ! -- George P. J. Turczynski, Computer Systems Engineer. Highland Logic Pty Ltd. ACSnet: george@highland.oz |^^^^^^^^^^^^^^^^^^^^^^^^| Suite 1, 348-354 Argyle St Phone: +61 48 683490 | Witty remarks are as | Moss Vale, NSW. 2577 Fax: +61 48 683474 | hard to come by as is | Australia. --------------------------- space to put them ! ---------------------------