Reputation: 265
I need to search in a directory of files which has pattern1 but not pattern2.
Upvotes: 1
Views: 2930
Reputation: 3748
You can add a grep to the first grep:
grep -r "this pattern" /path | grep -v "not this patten"
HTH
Francisco
Upvotes: 1
Reputation: 140786
grep pattern1 $(grep -L pattern2 *)
is probably the easiest way to do it, if I understand correctly what you want. -L
means "print just the names of all files that do not contain this pattern"; it's the inverse of -l
. This will not work correctly if you have files with whitespace or some other shell metacharacters in their names.
Upvotes: 1
Reputation: 2156
look at the -v flag to grep. You can pipe multiple calls to grep together, which is probably the simplest approach here. One to look for pattern1, and another to grep -v pattern2.
Upvotes: 2