How to Run grep With Multiple AND Patterns?

The grep is one of the most widely used commands to find or match the patterns in the text files or the command’s output. The grep or Global Regular Expression Print” can be used with different regular expressions and operator variations. One is the “AND” operator, which finds and prints specific data from a text file, and what if you want to use grep with multiple “AND” patterns? It is possible using the “-e” flag of the grep command.

This post will explain the working of grep to handle multiple AND patterns: 

  • How to Run grep With Multiple AND Patterns?
  • Syntax of grep Command
  • Examples

How to Run grep With Multiple AND Patterns?

The Global Regular Expression print or grep is an open-source, accessible, trendy command line utility. It is mainly used for finding the required text from the files using regular expressions. It plays quite a significant role in Linux as it can save lots of time to find specific text/numbers/strings or other related data.

A more detailed guide discussing every meaningful use of the grep command can be read here.

Syntax of the grep Command in Linux

The grep command, when used with multiple AND patterns, is used in the following syntax:


grep 'pattern1.*pattern2' filename

To view the list of options, use this help command:

$ grep --help

How to Run the grep Command With Multiple Patterns?

The “-e” flag is used with the grep to run with multiple AND patterns in this way:

grep -e pattern1 -e pattern2 [File-Name]

Here, the “-e” specifies each pattern to be searched and if you want to add more patterns to it, add the “-e” flag before it.

The file’s contents in each example will help you work multiple AND patterns in the grep command:

This is a sample file.
It has some lines of text.
There are a few words.
For example, we might look for the word "sample".
We can also search for multiple AND patterns at once.
Let's try searching for "sample" and "file" together.

Example 1: Find and View Text Using Multiple AND Patterns

In this example, the text “example”, and “sample” are to be found from the “file.txt.

$ grep -e example -e sample file.txt

As expected, the grep command filters multiple text patterns with the “-e” flag.

Example 2: Count the Lines of Occurances of the Specified Text

The “-e and -c” can be combined to count the lines where the text is found in the file in this way:

$ grep -ce example -ce sample file.txt

As expected, there are three lines in which the words “example” and “sample” are present.

Example 3: Match the Whole Word

If there is a lot of information in a text file and you want to view only the whole words, the “-e” flag can be used “-w” flag in this way:

$ grep -we examples -we sample file.txt

The above command displays all the information in the file but highlights only the whole words. The word “examples” is not equivalent to “example.”

Example 4: Find and View Text in Multiple Files

To find the occurrences of a word across multiple files (google in this instance), the “-e” flag with the “-n” flag is used in this format:

$ grep -ne example file.txt -ne bird file2.txt

The above command upon execution also displays the line numbers in which the pattern is matched.

Example 5: Find and View Text Using Multiple AND Patterns and Regular Expressions

to use the AND operator in grep with regular expressions, the “.* (dot-star)” is used as this:

$ grep 'few.*examples' file.txt

The above command matches any line that contains the word “few” followed by any number of characters and then the word “examples”. When both words are present in the same line, the order is ignored by it.


The “-e” flag runs grep with multiple AND patterns. It is used to specify an expression. Users can also combine flags such as “-w, -c” to search and view more precise data. The “-e and -n” flags are used for multiple files. All of the outputs can be examined in the images attached above. This guide explained the “-e” flag of the grep command to work with multiple AND patterns.