When searching for a string, grep will display all lines where the string is embedded in larger strings. To ignore case when searching, invoke grep with the -i option (or -ignore-case): grep -i 'fatal\|error\|critical' /var/log/nginx/error.log This means that the uppercase and lowercase characters are treated as distinct. 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īy default, grep is case sensitive. If the string you are searching includes spaces, enclose it in double quotation marks. In the following example, we are searching for all occurrences of the words fatal, error, and critical in the Nginx logĮrror file: grep 'fatal\|error\|critical' /var/log/nginx/error.log Literal strings are the most basic patterns.
QOOBAR MULTIPLE PERFORMERS HOW TO
When using extended regular expression, do not escape the | operator: grep -E 'pattern1|pattern2' file.įor more information about how to construct regular expressions, check our article Grep regex To interpret the pattern as an extended regular expression, invoke grep the -E ( or -extended-regexp) option. This is why we are escaping the OR operator ( |) with a slash. To keep the special meanings of the meta-characters, they must be escaped with a backslash ( \). When using basic regular expressions, the meta-characters are interpreted as literal characters. The syntax for searching multiple patterns using the grep basic regular expressions is as follows: grep 'pattern1\|pattern2' file.Īlways enclose the regular expression in single quotes to avoid the interpretation and expansion of the meta-characters by the shell. This operator has the lowest precedence of all regular expression operators. The alternation operator | (pipe) allows you to specify different possible matches that can be literal strings or expression sets. To search for multiple patterns, use the OR (alternation) operator. When no regular expression type is specified, grep interpret search patterns as basic regular expressions. GNU grep supports three regular expression syntaxes, Basic, Extended, and Perl-compatible. In this article, we’re going to show you how to use GNU grep to search for multiple strings or patterns. 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.