To grep multiple strings, pass each one with its own -e flag: grep -e ERROR -e WARN app.log prints every line containing ERROR or WARN. The same search works as an extended regular expression, grep -E 'ERROR|WARN' app.log.
That covers "any of these". Matching lines that contain all of several strings, or none of them, needs a different approach. The sections below cover each case.
Sample file used in the examples
The example output below was worked out by hand from this small app.log, so treat it as example output rather than a captured terminal session:
INFO server started on port 8080
WARN disk usage at 91%
ERROR connection refused: db01
error: retrying in 5s
INFO request id=42 completed
ERROR timeout after 30s on db01
grep for one of several strings (OR)
There are three equivalent ways to do it. Each treats the strings as alternatives, so a line is printed if it matches at least one.
grep -e ERROR -e WARN app.log
grep -E 'ERROR|WARN' app.log
grep 'ERROR\|WARN' app.log
Example output, identical for all three:
WARN disk usage at 91%
ERROR connection refused: db01
ERROR timeout after 30s on db01
The man page says of -e: "If this option is used multiple times or is combined with the -f (--file) option, search for all patterns given." Here "search for all patterns" means a line is selected when any one of them matches. The man page also says the pattern operand "is one or more patterns separated by newline characters", so a quoted multi-line argument works the same way.
The second form uses -E, which makes grep interpret the patterns as extended regular expressions. In that mode | is the alternation operator. The third form relies on GNU grep: its manual says "GNU BREs can use the escape sequences \?, \+, and \|, with the same meaning as their unescaped ERE counterparts." That backslash form is a GNU feature, so it may not work with the grep that ships on other systems. -e and -E are the portable choices.
The mistake that returns nothing
grep 'ERROR|WARN' app.log
This prints nothing. Without -E, the pattern is a basic regular expression, where | is a plain character, so grep looks for the literal text ERROR|WARN. The exit status is 1, which means "no lines selected". The fix is any of the three forms above. The grep examples article covers exit codes and the other common flags.
Mixing options with several patterns
Options apply to every pattern. -i ignores case for all of them, -w requires each match to form a whole word, and -x requires each match to be a whole line:
grep -iw -e error -e warn app.log
grep -x -e 'INFO request id=42 completed' -e 'WARN disk usage at 91%' app.log
The first prints all three problem lines plus the lowercase error: retrying in 5s. The second prints two lines, because -x only accepts lines that match a pattern from start to end. The % in the second command is only a literal character. If your search strings contain regex characters such as ., *, [ or $, add -F to treat every pattern as a fixed string: grep -F -e '1.2.3' -e '4.5.6' versions.txt.
grep for lines containing all the strings (AND)
grep has no AND operator between separate patterns. The usual answer is to chain greps, each one narrowing the output of the previous:
grep ERROR app.log | grep db01
Example output:
ERROR connection refused: db01
ERROR timeout after 30s on db01
The first grep keeps lines with ERROR, and the second keeps those that also contain db01. It works for any number of strings and in any order, since each stage only looks at its input. On a large file, put the string that rules out most lines first, because every later stage then reads less data.
If the strings must appear in a fixed order on the same line, one regular expression is enough: grep 'ERROR.*db01' app.log. The .* matches any run of characters between them. That pattern would not match a line where db01 comes before ERROR.
To do the same across a directory, run the first grep recursively and filter its output: grep -rn ERROR . | grep db01. The second grep sees the prefixed lines (for example ./app.log:3:ERROR connection refused: db01), so the file name and line number are part of the text it searches. A string that appears in a path could therefore match by accident.
grep for lines that contain none of the strings (NOT)
Add -v, which the man page defines as "Invert the sense of matching, to select non-matching lines". With several patterns it excludes a line if any one of them matches:
grep -v -e INFO -e WARN app.log
Example output:
ERROR connection refused: db01
error: retrying in 5s
ERROR timeout after 30s on db01
You can combine this with a positive search by piping: grep ERROR app.log | grep -v timeout keeps ERROR lines that do not mention a timeout. That is how you build "A and B but not C".
Read the patterns from a file
When the list is long, put one pattern per line in a file and pass it with -f. The man page describes it as "Obtain patterns from FILE, one per line." With a file patterns.txt containing:
ERROR
WARN
db01
run:
grep -f patterns.txt app.log
grep -F -f patterns.txt app.log
The second form is the better default when the file holds plain words, such as a list of hostnames or IDs, because -F stops any dots or brackets in them from acting as regex syntax. You can mix -f with -e in the same command, and grep searches for all the patterns from both.
Watch for blank lines in the file. GNU grep allows empty regular expressions, and an empty pattern matches every line, so a stray blank line makes the whole command print the entire input. The manual says an empty file, by contrast, "contains zero patterns, and therefore matches nothing". Use -v -f the same way to remove every line that matches any listed pattern.
Count or extract the matches
Two flags are useful once you have several patterns. -c counts matching lines, and -o prints only the matched part of each line, each on its own output line:
grep -c -E 'ERROR|WARN' app.log
grep -o -E 'ERROR|WARN|db01' app.log
The first prints 3. The second prints one match per line, in file order: WARN, ERROR, db01, ERROR, db01. Pipe the second through sort | uniq -c to see how often each string appears. Note that -c counts lines, so a line containing two of your strings still counts once.
Searching multiple strings in PowerShell
PowerShell's Select-String (alias sls) takes a list for -Pattern. Microsoft's documentation says the parameter "accepts multiple patterns in an array. If you specify more than one pattern, Select-String returns matches for each pattern."
Select-String -Path .\app.log -Pattern 'ERROR','WARN'
Select-String -Path .\app.log -Pattern 'ERROR|WARN'
Both return lines matching either word. Patterns are regular expressions, so | works without any extra switch, and matching is case-insensitive by default. Add -CaseSensitive for grep's default behaviour, or -SimpleMatch to treat patterns as literal text, like grep -F. For "all of these", chain two commands, as with grep:
Select-String -Path .\app.log -Pattern 'ERROR' | Select-String -Pattern 'db01'
The documentation notes that the receiving command searches only the text of the matched line, so the second stage works as expected. The -NotMatch switch is the equivalent of grep -v.
Which form to use
| You want | grep |
|---|---|
| Any of A, B | grep -e A -e B file or grep -E 'A|B' file |
| Both A and B, any order | grep A file | grep B |
| A then B, in that order | grep 'A.*B' file |
| Neither A nor B | grep -v -e A -e B file |
| A long list of strings | grep -F -f patterns.txt file |
For literal strings, prefer -e with -F. For anything that needs groups or anchors, use -E. If you are unsure whether a pattern behaves as you expect, run it on a three-line test file first and check the output before pointing it at a real log.
Frequently asked questions
- Why does grep 'foo|bar' find nothing?
- Without -E, grep reads the pattern as a basic regular expression, where | is an ordinary character. It searches for the literal text foo|bar. Use grep -E 'foo|bar', grep 'foo\|bar' (a GNU extension) or grep -e foo -e bar.
- Can grep match lines containing both of two strings?
- Yes. Chain two greps with a pipe: grep foo file | grep bar. The first keeps lines with foo, and the second keeps only those that also contain bar. The order of the two words in the line does not matter.
- How do I grep multiple strings from a file of patterns?
- Put one pattern per line in a file and run grep -f patterns.txt target.log. Add -F if the lines are plain text rather than regular expressions. An empty pattern file contains zero patterns and matches nothing.
- How do I search for multiple strings in PowerShell?
- Select-String accepts an array for -Pattern: Select-String -Path app.log -Pattern 'ERROR','WARN'. It returns lines matching any of the patterns. Matching is case-insensitive unless you add -CaseSensitive.