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

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

grep

Find lines matching a pattern in files or piped input — the workhorse of text search.

sed

Stream-edit text: substitute, delete lines, and edit files in place with -i.

xargs

Turn a list of items into command arguments — the glue between pipelines and commands.

Concepts involved: Regular expression

More how-to guides

Check disk space in Linux

See how much space is left, and find out what is using it.

Extract a .tar.gz archive

Unpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.

Free a port that is already in use

Find which process holds a port and stop it cleanly.

Find and replace text in files

Replace text in one file or across a whole project, safely.