How to start with:
cat * | sed 's/ /\n/g' | grep '^aaa$' | wc -l
as in the following decryption:
pax$ cat file1 this is a file number 1 pax$ cat file2 And this file is file number 2, a slightly larger file pax$ cat file[12] | sed 's/ /\n/g' | grep 'file$' | wc -l 4
sed converts spaces to newlines (you can include other spaces, such as tabs, with sed 's/[ \t]/\n/g' ). grep just gets those lines that have the desired word, and then wc counts those lines for you.
Now there may be cross cases where this script does not work, but in the vast majority of cases it should be fine.
If you need a whole tree (and not just one directory level), you can use somthing like:
( find . -name '*.txt' -exec cat {} ';' ) | sed 's/ /\n/g' | grep '^aaa$' | wc -l
paxdiablo May 26 '11 at 7:28 a.m. 2011-05-26 07:28
source share