Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I do

   grep "MyVariable = False" FormA.frm

   ... result1

   grep "MyVariable = True"  FormA.frm

   ... result2

How to write the grep command so that I can say something like

   grep "MyVariable = False" AND "MyVariable = True" FormA.frm
share|improve this question
You want to test whether a line contains Var1 = False AND Var2 = True? Or whether a file contains Var1 = False AND Var2 = True? Or something else? An example would help. – Mikel Mar 28 '12 at 15:40
I used AND by mistake. I meant "OR". – CodeBlue Mar 28 '12 at 15:43
up vote 10 down vote accepted

What you really want is "OR", not "AND". If "AND" is used, then logically, you'll get no lines (unless the line is something like "MyVariable = False...MyVariable = True".

Use "extended grep" and the OR operator (|).

grep -E 'MyVariable = False|MyVariable = True' FormA.frm
share|improve this answer
Yes, you're right. I wanted OR but incorrectly specified AND. Thanks. – CodeBlue Mar 28 '12 at 15:40
Incidentally, I noticed this works only with an uppercase "E" and not a lowercase "e". So grep arguments are case sensitive I suppose. – CodeBlue Mar 28 '12 at 15:41
@CodeBlue: you should get used to this: almost everything is case sensitive in Unix – enzotib Mar 28 '12 at 16:00
Ok thanks @enzotib . I come from a Windows background. – CodeBlue Mar 28 '12 at 16:03
It also works with egrep (which I use) instead of grep -E which is the more canonical. – Arcege Mar 28 '12 at 16:58

You should use

grep "MyVariable = \(False\|True\)" FormA.frm

where the \| sequence mean an alternative, and the delimiters \( and \) are for grouping.

share|improve this answer
Yup, this also works, although it is a bit confusing to me. Thanks. – CodeBlue Mar 28 '12 at 15:47

You can simply do

grep -E "MyVariable = False|MyVariable = True" FormA.frm
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.