Counting characters with unix utilities

Joe Habermann haberman at msi.umn.edu
Sat Sep 29 03:30:33 AEST 1990


george at hls0.hls.oz (George Turczynski) writes:

>OK, try this:

>	awk -F'A' '{ sum+= (NF-1) } END { print sum }' file

This is close.  Doesn't seem to work when the number of matches = 0, 
though.  In that case NF = 0 and the awk will return -1.

How about:

	awk -F'A' '{ if (NF > 0) sum += (NF-1) } END { print sum }' file

Joe Habermann / haberman at msi.umn.edu



More information about the Comp.unix.questions mailing list