Operating System - HP-UX
1752520 Members
4599 Online
108788 Solutions
New Discussion юеВ

Counting number of occurrences of a character

 
SOLVED
Go to solution
Ramsunder S
Occasional Advisor

Counting number of occurrences of a character

Hi,
I need to count the number of occurrences of a character in a file. Is there a Shell command to do it?

Regards,
Ram.
10 REPLIES 10
Steve Lewis
Honored Contributor

Re: Counting number of occurrences of a character

A trick in awk is to use the character as the field separator and count the fields on each line. Assume you want to count the number of x in myfile.

awk -Fx '/x/{t+=NF-1}END{print t}' myfile

This works with blank lines, lines without any x and with something else, also lines with several occurrances on a single line.

Steven Sim Kok Leong
Honored Contributor
Solution

Re: Counting number of occurrences of a character

Hi,

Using perl is most straightforward.

If not, then you can resort to the many creative ways of extracting the occurrence count. Apart from using awk, you can also make use of tr:

# echo `cat $FILE` | tr [$CHAR] ["\n"] | wc -l

Just minus 1 from the result and you get the number of occurrences of the character.

This works because:
1) echo `cat $FILE` sets the entire file on a single line.
2) tr [$CHAR] ["\n"] adds a newline everytime it encounters the character (by converting it to a newline).
3) wc -l counts the number of newlines which is no. of character occurrences + 1

If you want this in a SHELL script, it will be as follows:

count.sh
=====================================
#!/sbin/sh

single=`cat $1`
lines=`echo $single | tr [$2] ["\n"] | wc -l`
echo "Total occurrences of $2 in $1: `expr $lines - 1`"
=====================================

To run the script:

# count.sh /etc/profile e

Hope this helps. Regards.

Steven Sim Kok Leong
Justo Exposito
Esteemed Contributor

Re: Counting number of occurrences of a character

Hi,

Try this script (this is to count the number of "c" character in a file, you can change it easy for your character:

numcar=`sed -e 's/[a-b]//g' -e 's/[d-z]//g' filename |wc -c `
numlin=`cat filename | wc -l `
integer numcarok
(( numcarok = numcar - numlin ))
echo $numcarok

Hope this help,

Justo.

Help is a Beatiful word
Steve Lewis
Honored Contributor

Re: Counting number of occurrences of a character

You can't transpose the character you want into a new line and then use wc because wc will add in the number of newlines already in the file. For example file:
x
x
x
would return 5 or 6 instead of 3.

Steven Sim Kok Leong
Honored Contributor

Re: Counting number of occurrences of a character

Hi Steve,

You didn't read:

1) echo `cat $FILE` sets the entire file on a single line.

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: Counting number of occurrences of a character

Hi Justo,

I believe your script works on the assumption that the file contains only alphabets, no numbers and no special characters such as the space character or the dollar sign etc.

Hope this helps. Regards.

Steven Sim Kok Leong
Justo Exposito
Esteemed Contributor

Re: Counting number of occurrences of a character

Hi Steven,

Yes, your solution is better than mine.

Regards,

Justo.
Help is a Beatiful word
Carlos Fernandez Riera
Honored Contributor

Re: Counting number of occurrences of a character

awk ' BEGIN{RS="o"}^JEND{print NR-1}' 88


file 88:
hhohohohohhh
unsupported
Ramsunder S
Occasional Advisor

Re: Counting number of occurrences of a character

Thank you all for your replies. The transpose approach did the trick for me. I'm not very comfortable with Perl and awk and that's probably why I try to avoid them. Maybe I should get myself to learn them.

Regards,
Ram.