Vpdj Vpdj - 1 year ago 51
Bash Question

Counting number of occurrences in several files

I want to check the number of occurrences of, let's say, the character '[', recursively in all the files of a directory that have the same extension, e.g. *.c. I am working with the SO Solaris in Unix.

I tried some solutions that are given in other posts, and the only one that works is this one, since with this OS I cannot use the command

grep -o

sed 's/[^x]//g' filename | tr -d '012' | wc -c

Where x is the occurrence I want to count. This one works but it's not recursive, is there any way to make it recursive?

Answer Source

You can get a recursive listing from find and execute commands with its -exec argument.

I'd suggest like:

find . -name '*.c' -exec cat {} \; | tr -c -d ']' | wc -c

The -c argument to tr means to use the opposite of the string supplied -- i.e. in this case, match everything but ].

The . in the find command means to search in the current directory, but you can supply any other directory name there as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download