Counting characters with unix utilities
George Turczynski
george at hls0.hls.oz
Thu Sep 27 09:22:56 AEST 1990
In article <4002 at umbc3.UMBC.EDU>, rouben at 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 at 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 ! ---------------------------
More information about the Comp.unix.questions
mailing list