Table of Contents
This tip is about the how to Grep for Multiple Strings and Patterns. So read this free guide, How to Grep for Multiple Strings and Patterns step by step. If you have query related to same article you may contact us.
How to Grep for Multiple Strings and Patterns – Guide
Grep is a powerful utility available by default on UNIX-based systems. This name stands for Global Regular Expression Printing. Using the grep command, you can customize how the tool looks for patterns or multiple patterns in this case. You can grep multiple strings in different files and directories. This tool prints all lines that contain the words that you specify as the search pattern. In that guide, let’s show you how to use grep to find several words or string patterns. Learn how to use grep more effectively by following the examples in this tutorial.
Multiple Grep Patterns
GNU grep supports three regular expression syntaxes, Basic, Extended and Perl compatible. When no regular expression type is specified, grep interprets search patterns as basic regular expressions. To search for multiple patterns, use the OR (toggle) operator.
The toggle operator | (pipe) allows you to specify different possible matches which can be literal strings or sets of expressions. This operator has the lowest precedence of all regular expression operators. The syntax for searching multiple patterns using the basic grep regular expressions is as follows:
$ grep file ‘pattern1|pattern2’…
Always enclose the regular expression in single quotes to avoid the shell’s interpretation and expansion of metacharacters. When using basic regular expressions, metacharacters are interpreted as literal characters. To retain the special meanings of metacharacters, they must be escaped with a backslash (). That’s why we’re escaping the OR operator (|) with a slash.
To interpret the pattern as an extended regular expression, invoke grep the -E option (or –extended-regexp). When using the extended regular expression, do not escape the | operator:
$ grep -E file ‘pattern1|pattern2’…
For more information about how to build regular expressions, check out our Grep regex article.
Grep Multiple Strings
Literal strings are the most basic patterns. In the following example, we are looking 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
By default, grep is case sensitive. This means that uppercase and lowercase characters are treated as distinct. To ignore case when searching, invoke grep with the -i (or –ignore-case) option:
$ grep -i ‘fatal|error|critical’ /var/log/nginx/error.log
When looking for a string, grep will output all lines where the string is embedded in larger strings. So if you were looking for “error”, grep will also print the lines where “error” is embedded in words, such as “no errors” or “anti-terrorist”.
To return only lines where the specified string is a whole word (between characters that are not words), use the -w ( or –word-regexp) option:
$ grep -w ‘fatal|error|critical’ /var/log/nginx/error.log
Final note
I hope you like the guide How to Grep for Multiple Strings and Patterns. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.