How to Grep for Multiple Strings and Patterns

Posted 

3 min read

grep is a powerful command-line tool that allows you to searches one or more input files for lines that match a regular expression and writes each matching line to standard output.

In this article, we’re going to show you how to use GNU grep to search for multiple strings or patterns.

Grep Multiple Patterns

GNU grep supports three regular expression syntaxes, Basic, Extended, and Perl-compatible. When no regular expression type is specified, grep interpret search patterns as basic regular expressions.

To search for multiple patterns, use the OR (alternation) operator.

The alternation operator | (pipe) allows you to specify different possible matches that can be literal strings or expression sets. This operator has the lowest precedence of all regular expression operators.

The syntax for searching multiple patterns using the grep basic regular expressions is as follows:

grep 'pattern1\|pattern2' file...

Always enclose the regular expression in single quotes to avoid the interpretation and expansion of the meta-characters by the shell.

When using basic regular expressions, the meta-characters are interpreted as literal characters. To keep the special meanings of the meta-characters, they must be escaped with a backslash (\). This is why we are escaping the OR operator (|) with a slash.

To interpret the pattern as an extended regular expression, invoke grep the -E ( or --extended-regexp) option. When using extended regular expression, do not escape the | operator:

grep -E 'pattern1|pattern2' file...

For more information about how to construct regular expressions, check our article Grep regex.

Grep Multiple Strings

Literal strings are the most basic patterns.

In the following example, we are searching for all occurrences of the words fatal, error, and critical in the Nginx log error file:

grep 'fatal\|error\|critical' /var/log/nginx/error.log

If the string you are searching includes spaces, enclose it in double quotation marks.

Here is the same example using the extended regular expression, which eliminates the need to escape the operator |

grep -E 'fatal|error|critical' /var/log/nginx/error.log

By default, grep is case sensitive. This means that the uppercase and lowercase characters are treated as distinct.

To ignore case when searching, invoke grep with the -i option (or --ignore-case):

grep -i 'fatal\|error\|critical' /var/log/nginx/error.log

When searching for a string, grep will display all lines where the string is embedded in larger strings. So if you were searching for “error”, grep will also print the lines where “error” is embedded in larger words, such as “errorless” or “antiterrorists”.

To return only those lines where the specified string is a whole word (enclosed by non-word characters), use the -w ( or --word-regexp) option:

grep -w 'fatal\|error\|critical' /var/log/nginx/error.log

Word characters include alphanumeric characters (a-z, A-Z, and 0-9) and underscores (_). All other characters are considered as non-word characters.

For more details about grep options, visit our article Grep command.

Conclusion

We have shown you how to grep to search multiple patterns, strings, and words.

If you have any questions or feedback, feel free to leave a comment.