How to Grep for Multiple Strings and Patterns

grep is a powerful command-line tool that searches one or more input files for lines that match a regular expression and writes each matching line to standard output.
This guide explains the different ways to grep for multiple patterns, including the -e flag, alternation operators, and reading patterns from a file.
Use the -e Flag
The clearest way to search for multiple patterns is to pass each one with a separate -e option:
grep -e 'pattern1' -e 'pattern2' fileEach -e argument adds one pattern. grep prints any line that matches at least one of them.
For example, to search for fatal, error, and critical in the Nginx error log
:
grep -e 'fatal' -e 'error' -e 'critical' /var/log/nginx/error.logThis form is the most readable and works with both basic and extended regular expressions.
Use the Alternation Operator
GNU grep supports three regular expression syntaxes: Basic (BRE), Extended (ERE), and Perl-compatible (PCRE). By default, grep interprets search patterns as basic regular expressions.
Basic Regular Expressions
In basic regular expressions, the | alternation operator must be escaped with a backslash:
grep 'pattern1\|pattern2' fileAlways enclose the pattern in single quotes to prevent the shell from interpreting the meta-characters.
To search for fatal, error, or critical:
grep 'fatal\|error\|critical' /var/log/nginx/error.logExtended Regular Expressions
With the -E (--extended-regexp) option, grep uses extended regular expressions where | does not need to be escaped:
grep -E 'pattern1|pattern2' fileThe same search using extended syntax:
grep -E 'fatal|error|critical' /var/log/nginx/error.logegrep is a traditional alias for grep -E and produces identical output:
egrep 'fatal|error|critical' /var/log/nginx/error.logFor more information about constructing regular expressions, see the grep regex guide .
Read Patterns from a File
When you have many patterns to match, use the -f option to read them from a file instead of listing them on the command line:
grep -f patterns.txt fileEach line in the patterns file is treated as a separate pattern. For example:
fatal
error
criticalThen run:
grep -f patterns.txt /var/log/nginx/error.logThis approach is useful for recurring searches or when the pattern list is maintained separately.
Case-Insensitive and Whole-Word Matching
These options work with all the methods above.
To ignore case when searching, add the -i (--ignore-case) option:
grep -i -e 'fatal' -e 'error' /var/log/nginx/error.logBy default, grep matches the pattern anywhere in a line. Searching for error will also match words like errorless. To match only whole words, use the -w (--word-regexp) option:
grep -w -E 'fatal|error|critical' /var/log/nginx/error.logWord boundaries are defined by non-alphanumeric, non-underscore characters.
Quick Reference
| Task | Command |
|---|---|
Multiple patterns with -e | grep -e 'p1' -e 'p2' file |
| Alternation, basic regex | grep 'p1|p2' file |
| Alternation, extended regex | `grep -E ‘p1 |
| Patterns from a file | grep -f patterns.txt file |
| Case-insensitive | grep -i -e 'p1' -e 'p2' file |
| Whole-word match | `grep -w -E ‘p1 |
| Extended regex alias | `egrep ‘p1 |
For a printable quick reference, see the grep cheatsheet .
FAQ
What is the difference between grep -e and grep -E?-e adds a pattern to the list — you can use it multiple times. -E switches the regex engine to extended mode, which changes how operators like | are interpreted. Both can be combined: grep -E -e 'p1' -e 'p2' file.
Which method should I use to grep multiple patterns?
Use -e for a small number of fixed strings — it is the most readable. Use -E 'p1|p2' when patterns involve regular expressions. Use -f when the pattern list is long or needs to be reused across commands.
Can I grep for multiple patterns and exclude others at the same time?
Yes. Pipe the output to a second grep -v to exclude lines: grep -E 'error|fatal' file | grep -v 'debug'. For more on excluding patterns, see the grep exclude guide
.
Does -e work with -i and -w?
Yes. All standard grep options combine freely: grep -i -w -e 'error' -e 'fatal' file performs a case-insensitive, whole-word search for both patterns.
What is egrep?egrep is a traditional alias for grep -E. It is available on most Linux systems but is considered deprecated in favour of grep -E. Both produce identical output.
Conclusion
The most readable way to grep for multiple patterns is with the -e flag. For regex-heavy searches, use -E with the | alternation operator. For large pattern lists, use -f to read patterns from a file.
For more grep options and examples, see the grep command guide
and the grep cheatsheet
.
If you have any questions, feel free to leave a comment below.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page