Grep

How to use logical AND in grep?

Questions by nayakss   answers by nayakss

Showing Answers 1 - 3 of 3 Answers

UncaAlby

  • Dec 12th, 2008
 

By logical "and" I presume you mean that you wish to see lines that contain both "expressionOne" AND "expressionTwo".

Just pipe the standard output of one "grep" command into the standard input of a second "grep" command.

E.g.:

echo "Good Bad and Ugly" | grep "Good" | grep "Bad"

will print the line.  It is, paradoxically, both "Good" AND "Bad".

echo "All Good Men Must Sleep" | grep "Good" | grep "Bad"

will print nothing.  "Good" AND "Bad" is false.

A thornier question (which you didn't ask) is how to do a logical "OR".

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions