-1

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"?

honk
  • 9,137
  • 11
  • 75
  • 83
  • 2
    please show some [mcve], otherwise we cannot know how your data is structured. – fedorqui Jun 16 '16 at 08:29
  • Possible duplicate of [Finding all files containing a text string on Linux](http://stackoverflow.com/questions/16956810/finding-all-files-containing-a-text-string-on-linux) – Inian Jun 16 '16 at 08:40

1 Answers1

1

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

neric
  • 3,927
  • 3
  • 21
  • 25