Ashutosh Mishra Ashutosh Mishra - 4 months ago 5
Linux Question

How can I get the number of XML files containing a specific phrase?

I have a directory containing XML files which contain information about cartoons. For example, a cartoon can be a simple "Cartoon" or a "Cartoon Sketch" and so on.

When I apply the following command, then I get all XML files containing the phrase "Cartoon Sketch":

grep -R --files-with-matches --include="*.xml" "Cartoon Sketch" /path/to/dir | nl

But when I change the above command to get all XML files containing the word "Cartoon" as follows:

grep -R --files-with-matches --include="*.xml" "Cartoon" /path/to/dir | nl

then I get also all the files containing the phrase "Cartoon Sketch".

How can I limit the result just to files containing "Cartoon" but not "Cartoon Sketch"?


Add another grep -v to excludes lines matching "Sketch"

grep -R --files-with-matches --include="*.xml" "Cartoon" /path/to/dir | grep -v "Sketch" | wc -l