born
born

Reputation: 265

Is it possible to use "AND " and "NOT" condition in the same grep command

I need to search in a directory of files which has pattern1 but not pattern2.

Upvotes: 1

Views: 2930

Answers (3)

Francisco Zarabozo
Francisco Zarabozo

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

zwol
zwol

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

Randy Howard
Randy Howard

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

Related Questions