

When searching for a string, grep will display all lines where the string is embedded in larger strings. $ grep -i ‘fatal|error|critical’ /var/log/nginx/error.log To ignore case when searching, call grep with the -i (or –ignore-case) option: This means that uppercase and lowercase characters are treated as different. $ grep ‘fatal|error|critical’ /var/log/nginx/error.logīy default, grep is case sensitive. In the following example, we search for all occurrences of the words fatal, error, and Critical in the Nginx error log file: String literals are the most basic patterns.
#Grep for multiple strings how to#
When using extended regular expressions, do not escape the | operator:įor more information on how to build regular expressions, see our Grep regex article. To interpret the pattern as an extended regular expression, call the -E (or –extended-regexp) option to grep. That’s why we’re escaping the OR operator (|) with a forward slash. To keep the special meanings of metacharacters, they must be escaped with a backslash (). When using basic regular expressions, metacharacters are interpreted as literal characters. The syntax for searching for multiple patterns using the basic grep regular expressions is as follows:Īlways enclose the regular expression in single quotes to prevent shell interpretation and expansion of metacharacters. This operator has the lowest precedence of all regular expression operators. The alternation operator | (pipe) allows you to specify different possible matches which can be literal strings or sets of expressions. To search for multiple patterns, use the OR (alternating) operator. When no regular expression type is specified, grep interprets search patterns as basic regular expressions. GNU grep supports three regular expression syntaxes, basic, extended, and Perl-compatible. Learn how to use grep most efficiently by following the examples in this tutorial. In this guide, we will show you how to use grep to find multiple words or string patterns.

This tool prints all the lines that contain the words that you specify as the search pattern. You can grep multiple strings in different files and directories. Using the grep command, you can customize how the tool looks for patterns or multiple patterns in this case. This name stands for global regular expression printing. Grep is a powerful utility available by default on UNIX-based systems. To match only specific words when matching across multiple lines you can use regular expression tools to match one words.Check How to Grep for Multiple Strings and Patterns This means that simply typing in fail will also match failure.

It is important to know that the “strings” following the grep command will match the document based on the rules of regular expression.

Alternatively, tools such as awk or sed will start from the first instance of from but finish at the first instance of to. This will likely affect the output you expected, especially when there may be multiple instances of from or to in your document. When using grep across multiple lines it is important to be aware that the command will get both the first instance of the from word and will get everything up until the last instance of the to word. Grep for single line to the final word in another lineĬommon “gotchas” when using grep across multiple lines grep will use the first and last instances of the words If you want to simply print out file names that have lines that have matches with the regular expression then you can alter the -o flag to -l which will list all matching file names. * will match everything, including new lines, up until to because of the addition of (?s) into the regular expression. (?s) activate PCRE_DOTALL which means that “.” finds any character or a new line.The complication however is that will also add a trailing zero byte character which can cause additional problems. -o prints only the matching strings as otherwise the entire file will be printed.Essentially this allows grep to treat the file as a whole line as opposed to multiple lines -z treats the input as a set of lines, each being terminated by a zero byte instead of a new line.-P uses Perl compatible regular expression (PCRE).This will then become available as ggrep.
