How-to

Grep for Multiple Strings: OR, AND and Lists

grep matches one pattern by default, but one pattern can hold many alternatives. OR is built in; AND takes a pipe or a lookahead.

How do I match any of several strings?

Three equivalent ways:

grep -e 'timeout' -e 'refused' -e 'reset by peer' /var/log/app/client.log
grep -E 'timeout|refused|reset by peer' /var/log/app/client.log
grep 'timeout\|refused\|reset by peer' /var/log/app/client.log

Repeated -e is the clearest and needs no escaping. -E with | is what most admins type. The third form uses GNU grep's basic regex with escaped bars; it works on Linux but not on every Unix grep, so prefer the first two in portable scripts.

How do I read patterns from a file?

printf '%s\n' 'disk full' 'read-only file system' 'I/O error' > /etc/alert-patterns.txt
grep -F -f /etc/alert-patterns.txt /var/log/syslog

-f reads one pattern per line; -F treats them as fixed strings, which is fast even with thousands of lines. A list of blocked IPs, bad user agents or alert phrases fits naturally into this form, and anyone can update the list without touching the script.

How do I require all strings on the same line?

grep has no AND operator, so chain filters. Each grep keeps only lines that passed the previous one:

grep 'POST' access.log | grep '/checkout' | grep ' 502 '

With Perl regex, lookaheads express AND in one pattern, regardless of order:

grep -P '^(?=.*POST)(?=.*/checkout)(?=.* 502 )' access.log

When the order is known, a single extended regex is simplest: grep -E 'POST.*/checkout.* 502 '.

How do I match whole words from a list?

grep -wE 'root|admin|test' /var/log/auth.log
grep -w -F -f usernames.txt /var/log/auth.log

-w requires each match to stand as a whole word, so admin does not match administrator. Combined with -F -f, it checks a list of exact names quickly.

How do I count each term separately?

Print only the matched part with -o, then group:

grep -oE 'timeout|refused|reset by peer' client.log | sort | uniq -c | sort -rn

The output tells you which error dominates, which is usually more useful than one combined count. Counting is covered in more depth in Count matches with grep.

How do I search several strings across many files?

grep -rlE 'mysql_connect|mysql_query' /srv/legacy --include='*.php'
grep -rnwE 'eval|base64_decode|gzinflate' /var/www --include='*.php' | head -30

The first finds files still using removed PHP functions before an upgrade. The second is a quick look for code often seen in compromised sites; legitimate uses exist, so read each hit before acting. Skipping vendor folders and other noise is covered in Exclude with grep.

Which form should I use when?

NeedForm
Any of a few strings-e a -e b or -E 'a|b'
Any of many fixed strings-F -f list.txt
All strings, any orderChained greps, or -P lookaheads
All strings, known order-E 'a.*b'
Whole words onlyAdd -w
Which term matched how often-o with sort | uniq -c

Are there performance limits?

Rarely with grep. GNU grep handles alternations and pattern files efficiently, and -F with large lists uses a fast multi-string algorithm. Logs of several gigabytes search in seconds on NVMe storage. For whole repositories, ripgrep's rg -e a -e b does the same in parallel across cores, which pays off on many-core machines. The broader grep reference is The grep command.

How do I match strings that may span two lines?

grep works line by line, so a phrase split across a line break will not match. GNU grep's -z treats the whole input as one record, letting a Perl pattern cross newlines: grep -Pzo 'BEGIN CERTIFICATE[\s\S]*?END CERTIFICATE' bundle.pem extracts the first certificate from a bundle. For anything more complex, a short awk or Python script is clearer.

Keep Reading

The grep command

grep from basics to power use: case, whole words, line numbers, context, recursion, regex flavors, only-matching output and speed tips.

Open

Count matches with grep

Count matching lines, every match, matches per file and per value with grep -c, -o and a few pipes, plus common counting traps.

Open

Exclude with grep

Leave things out of grep results: invert matches, skip files and folders by pattern, ignore binaries and comments, and filter noisy log lines.

Open

Set a curl user agent

Change or remove the User-Agent header curl sends, test how your own server treats different clients and read agents in your logs.

Open

FAQ

Quick Answers

Use grep -e first -e second, or grep -E 'first|second'.

Chain two greps with a pipe, or use grep -P with lookaheads.

Yes. Use -f with the file, and -F when the patterns are fixed strings.

Yes. Add -w so each pattern must appear as a whole word.

Use grep -o with the patterns, then sort | uniq -c.