Bryce Johnson
Bryce Johnson

Reputation: 6901

How can I exclude and include in the same grep?

I'm writing a shell script that finds occurences a string ($string) of a certain filetype ($ext) in the current working directory ($cwd).

I also want to exclude all files that contain a certain string ("mbundle").

How would I amend what I have, to do that?

grep -nrwl $string --include=\*."$ext" "$cwd"

Sorry, I'm still pretty new to grep and regex/globbing patterns etc

Upvotes: 1

Views: 782

Answers (1)

anubhava
anubhava

Reputation: 784898

If your grep supports -P (PCRE) option then you can use negative lookahead to avoid matching mbundle:

grep -P -nrwl "(?!.*?mbundle)$string" --include=\*."$ext" "$cwd"

Upvotes: 1

Related Questions