How to Add an “OR” Condition in grep?

In Linux, the “grep” is the built-in utility used for searching or matching the text or regular expressions in a file. The grep can be used with various operators, i.e., OR, AND, and NOT to fulfill the specific condition and then perform the operation. In particular, the OR operator is used with multiple expressions to check for the specific condition and the expression satisfying the specific condition is selected for further proceedings.

This article will present how the user can use the OR condition in the “grep” command.

How to Add OR Condition in the grep Command?

The syntax for using the OR condition in the grep command is given below:


$ grep [Options] "<Pattern1>\|<Pattern2>\|<Pattern3>" [Path of the File]

Use the “grep” keyword, “Options” for the grep command, type the patterns ending with “\” separated by pipe command and the file name to search.

Before proceeding, have a look at the following file which will be carried out in the upcoming examples: 

$ cat File.txt

In the above file, we have details of the employees having their names, designations, departments, and salaries.

Example 1: Displaying the Record Using grep OR 

To use the “OR” operator, specify the patterns and files to search. As the following command will match the row having “Developer” or “Technology” in it.

$ grep 'Developer\|Technology' File.txt

The 2 rows have been printed with the given pattern.

Example 2: grep OR With “E” Flag

The use of the “E” flag (Extended regex) in the command does not require the “\” to match the patterns, just type patterns separated with the “|” operator:

$ grep -E 'Manager|Sysadmin' File.txt

The “Manager” and “Sysadmin” rows have been listed.

Example 3: grep OR With “e” Flag

The user can also use the “e” flag that will give the same results as the above examples are showing. Separate the patterns with “e” options as given in the below example:

$ grep -e Manager -e Sysadmin File.txt

The matching expressions have been listed (same as the above example 2).

How to Add OR in egrep Command?

The “egrep” is the extended version of the grep command that provides the extra features of using the OR condition without the “\”, “E”, or “e” option. It allows the user to search for the content in the file using extended regular expressions. The “egrep” command follows the syntax mentioned below:

$ egrep '<Pattern1>|<Pattern2>' [Path of the File]

Check the below example in which no option or backslash is written:

$ egrep 'Marketing|Sales' File.txt

The given patterns (Marketing and Sales) have been printed


To use the OR condition in the grep command, specify the patterns separated with “\”, or use the Flags “E” or “e” in the given syntax. Apart from the grep utility, the user can also use the “egrep” utility for using the OR condition to get the same results. This write-up has illustrated how you can add the OR condition in the grep command.