How to Search for Multiple Patterns with grep
Match several words with grep using -e or extended regular expressions.
Log searches often need more than one term. grep supports alternatives with extended regular expressions or repeated -e options.
Step by step
1 Match either pattern
$ grep -E "error|warning" app.log The | operator means OR in an extended regular expression.
2 Use several literal patterns
$ grep -e ERROR -e FATAL app.log Repeat -e to provide independent patterns.
3 Ignore case too
$ grep -Ei "error|warning" app.log Combine -E and -i for flexible matching.
4 Print only the match
$ grep -Eo "error|warning" app.log The -o option prints only the matching part.
Tips worth knowing
- Quote regex expressions.
- Use `grep -f patterns.txt file` for a pattern list.
- Use -F when patterns should be literal.
Frequently asked questions
How do I grep for two words?
Use `grep -E "word1|word2" file` for OR matching.
What does grep -E do?
It enables extended regular expressions, including `|`.
The commands behind it
Find lines matching a pattern in files or piped input — the workhorse of text search.
sedStream-edit text: substitute, delete lines, and edit files in place with -i.
xargsTurn a list of items into command arguments — the glue between pipelines and commands.
Concepts involved: Regular expression
More how-to guides
See how much space is left, and find out what is using it.
Extract a .tar.gz archiveUnpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.
Free a port that is already in useFind which process holds a port and stop it cleanly.
Find and replace text in filesReplace text in one file or across a whole project, safely.