Grep Multiple Patterns: Match Strings, Words, and Regex

When checking logs or scanning text files, you often need to search for several possible terms at once. For example, an application log might contain fatal, error, or critical, and any of those lines can point to the same problem.
The grep command can match multiple patterns in one search. This guide explains how to use the -e flag, literal string matching, alternation operators, and pattern files.
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.
If you want to match literal strings instead of regular expressions, add -F (--fixed-strings):
grep -F -e 'pattern1' -e 'pattern2' fileThis is the safer choice when the strings contain characters that have special meaning in regular expressions, such as ., *, [, or ].
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.logYou may also see the older egrep command in existing scripts. It is equivalent to grep -E, but grep -E is the preferred form for new commands.
For 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
For a printable quick reference, see the grep cheatsheet .
| Task | Command |
|---|---|
Multiple patterns with -e | grep -e 'p1' -e 'p2' file |
| Multiple literal strings | grep -F -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 |
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 patterns because it is the most readable. Use -F -e when you need literal string matching. 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 favor 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
.
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