Operating System - HP-UX
1833729 Members
2573 Online
110063 Solutions
New Discussion

number of occurances of a character

 
Smaran
Occasional Advisor

number of occurances of a character

How do I count the number of occurances
of a particular character in an unix file?

13 REPLIES 13
Steven E. Protter
Exalted Contributor

Re: number of occurances of a character

cat filename | wc -m

That will give you a count

count=$(cat filename | wc -m)

echo $count

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Simon Hargrave
Honored Contributor

Re: number of occurances of a character

If you don't want to do it interactively, you could open the file in vi, and eg to count the "a"'s: -

:%s/a/A/g

It will tell you "437 substitutions". Then don't save the file.

I had hoped that "ed" would report a similar output so you could do: -

ed file <1,$s/a/A/g
q
EOF

and read the output, but it seems not to. This should get you started though.
H.Merijn Brand (procura
Honored Contributor

Re: number of occurances of a character

# man freq

# freq file

And in perl (not tested)
# perl -ne'$c{$_}++ for split//;END{for(sort keys%c{printf"%3d %2s %6d\n",ord$_,$_ lt" "?"^".($_|"@"):$_,$c{$_}}}' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: number of occurances of a character

Ahh, ohh, I counted the number of occurences of every single character in the file :/

a5:/u/usr/merijn 109 > freq .Xdefaults | grep -w a
|soh 0|! 91|A 8|a 335|
a5:/u/usr/merijn 110 > perl -nle'END{print$a}$a+=(tr/a/a/)' .Xdefaults
335
a5:/u/usr/merijn 111 >

Enjoy, Have Fun
Enjoy, Have FUN! H.Merijn
Rodney Hills
Honored Contributor

Re: number of occurances of a character

Here is how it can be done with "awk".

echo "abcadefgabf\nkhaeeaf" | awk -Fa '{s=s+NF-1};END{print s}'

This will count number of "a" in file.

Here is how it can be done with "perl".

echo "abcadefgabf\nkhaeeaf" | perl -ne '$n=s/a//g;$s+=$n;END{print $s,"\n";}'

HTH

-- Rod Hills
There be dragons...
Smaran
Occasional Advisor

Re: number of occurances of a character

perl use is throwing the following errors
syntax error in file /tmp/perl-ea02209 at line 1, next 2 tokens "END {"
syntax error in file /tmp/perl-ea02209 at line 2, next token "}"
Execution of /tmp/perl-ea02209 aborted due to compilation errors.

H.Merijn Brand (procura
Honored Contributor

Re: number of occurances of a character

Sounds like you have /usr/contrib/bin in your $PATH before (if any) a more modern perl5:

a5:/u/usr/merijn 112 > /usr/contrib/bin/perl -nle'END{print$a}$a+=(tr/a/a/)' .Xdefaults
syntax error in file /tmp/perl-ea03721 at line 1, next token "."
Execution of /tmp/perl-ea03721 aborted due to compilation errors.
Exit 2
a5:/u/usr/merijn 113 >

That's from perl4

perl4 should be happy with this:

a5:/u/usr/merijn 124 > /usr/contrib/bin/perl -le 'while(<>){$a+=y/a/a/}print$a' .Xdefaults
335
a5:/u/usr/merijn 125 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
RAC_1
Honored Contributor

Re: number of occurances of a character

For procura,

I do not have freq command. I am on 11i.
where is that command.

Anil
There is no substitute to HARDWORK
Fred Ruffet
Honored Contributor

Re: number of occurances of a character

echo $(( $(sed "s/[^a]//g" /etc/passwd|wc -m) - $(cat /etc/passwd|wc -l) ))

-> Count a on each line - number of lines

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
H.Merijn Brand (procura
Honored Contributor

Re: number of occurances of a character

Comes from System III times.
Old but VERY useful (when you need it)

Source attached

I'm so used to having it that I forgot It was from source.

On some systems (like Linux) you'll need an additional

#include

Enjoy, Have FUNE H.Merijn
Enjoy, Have FUN! H.Merijn
john korterman
Honored Contributor

Re: number of occurances of a character

Hi,
you can do it with a little workaround - at least I think so. Example of an input file:

# cat infile
abc
defg
hij
kl
amn
opq
raa
sta

Count all chars of infile:
# wc -c infile
32 infile

Now fake a deletion of the char in question, e.g. "a", and count the remaining:
# tr -d a 27

Just a suggestion...

John K.
it would be nice if you always got a second chance
Smaran
Occasional Advisor

Re: number of occurances of a character

Thanks
Gilbert Standen_1
Frequent Advisor

Re: number of occurances of a character

Mr. Hills, the awk method for finding "a" was very useful for me -- it is appreciated.

I modified the awk to find the number of occurrences of "/". I find that the perl does not seem to be able to handle this as written (is further modification required?)Here are the versions of your code with "/" substituted for "a" (see post from Rodney Hills above for original code):

Works:
#echo "/m03/oracle/oradata" | awk -F/ '{s=s+NF-1};END{print s}'
3

Does Not Work:
#echo "abcadefgabf\nkhaeeaf" | perl -ne '$n=s/\//g;$s+=$n;END{print $s,"\n";}'
Substitution replacement not terminated at -e line 1.

Also Does not Work (gives "incorrect" result):
#echo "abcadefgabf\nkhaeeaf" | perl -ne '$n=s/\\//g;$s+=$n;END{print $s,"\n";}'
0

Is additional code required in the perl example to handle special characters like "$", ".", "/" etc. ?
Gil


If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums