4

How do I count all occurrences of a particular string in all files in a folder?

For example,

1.txt: 'hahaha hehe'
2.txt: 'ha hee'

I would like to count all occurrences of 'ha' in these files.

I tried:

grep "ha" * | wc-l 

But it just gives me individual occurrences in file output

2 Answers 2

5

You're close. To get a total count of all occurrences of "ha" within all .txt files in a folder:

grep -o "ha" *.txt | wc -l

From man grep:

-o, --only-matching
       Print only the matched (non-empty) parts of a matching line, with
       each such part on a separate output line.

This works because each match is printed on a separate line, thus allowing wc -l to count all of them.

By default, however, grep only finds the first occurrence on a line and outputs the whole line. Likewise, option -c only finds the first occurrence in all lines, then outputs how many lines had 1 (or more) matches.

EDIT:

Here is a way to print the total number of occurrences within each individual file (with filenames):

find *.txt -printf 'echo "$(grep -o "ha" %p | wc -l) %p";' | sh

#Example output
3 file1.txt
1 file2.txt

Explanation:

find *.txt - finds .txt files

-printf - prints everything between the single-quotes (formatted) to standard output, replacing occurrences of %p with find's output (file names)

$(grep -o "ha" %p | wc -l) - works as above

| sh - the output from -printf (which are commands) are piped to a shell and executed

Note that printf is invoked once per filename.

3
  • How about if I want to find out number of occurrences in each individual file
    – aceminer
    Mar 12, 2016 at 5:55
  • Guys, is there some reason for why you cannot use the features of grep? grep -H -c ip6 * seems to me what's requested
    – user556625
    Mar 12, 2016 at 22:22
  • @GombaiSándor: -H prints the filenames but the -c option still only finds one match per line. OP wants all Mar 15, 2016 at 0:08
4

Instead of using grep, try use ag -c ha SilverSearcher:

1.txt:3
2.txt:1

It is more faster! If you are using ubuntu, you can install package silversearcher-ag.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .