How to combine two grep statements and display their results together?

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" OR "MyVariable = True" FormA.frm

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

Solution 2

You should use

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

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

Solution 3

You can simply do

grep -E "MyVariable = False|MyVariable = True" FormA.frm

Solution 4

To answer in another way than what has already been said…

You can also specify several matches to grep, by specifying the -e option several times

% grep -e "MyVariable = True" -e "MyVariable = False" FormA.frm
 ... result1
 ... result2

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply