


To follow along with this guide, you will need access to a computer running a Linux-based operating system. In this tutorial, you will explore the grep command’s options, and then you’ll dive into using regular expressions to do more advanced searching. This seemingly trivial program is extremely powerful its ability to sort input based on complex rules makes it a popular link in many command chains. This means that you can use grep to check whether the input it receives matches a specified pattern. The name grep stands for “global regular expression print”. All other characters are considered as non-word characters.The grep command is one of the most useful commands in a Linux terminal environment. Word characters include alphanumeric characters (a-z, A-Z, and 0-9) and underscores (_). 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 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”. 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. 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.
