Hello everyone, I’m Liang Xu!
Today, I’m going to introduce you to a very useful technique, which is to use the grep command to find multiple strings.
Briefly, the grep command is a powerful command-line tool that searches one or more input files for text matching a regular expression, and then writes out each matching text in standard output format.
So, let’s share with you how to use grep for multiple search modes and grep for multiple strings:
Multi-mode Grep command
The grep command supports three regular expression syntax: Basic, Extended, and Perl-compatible. When no regular expression type is specified, the grep command defaults the search mode to basic regular expression.
To search for multiple matching patterns, use the OR (alternation) operator. We can use the OR operator | (pipe) to specify different match, the match can be a text string, can also be set expressions. It is worth noting that this operator has the lowest precedence of all regular expression operators.
The syntax of using the basic regular expression of the grep command to search for multiple matching patterns is as follows:
$ grep 'pattern1\|pattern2' filename
Copy the code
Note that you should always enclose the regular expression in single quotation marks, because the content inside single quotation marks is printed as is, and the content enclosed in single quotation marks is not replaced by constants or variables.
With basic regular expressions, metacharacters are interpreted as literal characters. To preserve the special meaning of metacharacters, they must be escaped with a backslash (\). This is why we want to escape the OR operator (|).
To interpret the pattern as an extended regular expression, call the grep -e (or –extended-regexp) option. When using extended regular expression, don’t need to escape the OR operator (|) :
$ grep -E 'pattern1|pattern2' file
Copy the code
The Grep command searches for multiple strings
In general, we think of literal strings as the most basic pattern.
For an example, we will search for all fatal, error, and critical strings that occur in a user log error file. The syntax is as follows:
$ grep 'fatal\|error\|critical' /var/log/nginx/error.log
Copy the code
Also note that if the string you are searching for contains Spaces, you need to enclose it in double quotation marks.
Here is the same example of using an extended regular expression that does not require an escape character:
$ grep -E 'fatal|error|critical' /var/log/nginx/error.log
Copy the code
By default, the grep command is case sensitive. To ignore case when searching, call grep plus -i (or –ignore-case) as shown in the following example:
$ grep -i 'fatal|error|critical' /var/log/nginx/error.log
Copy the code
When you want to search only for a word, such as error, grep will output all the lines that contain the error string, that is, it will output all the lines that contain the error word, It also outputs lines that contain non-error words like errorless or Antiextinguish, which is extremely inconvenient.
So to return only lines where the specified string is a whole word, or lines enclosed by non-word characters, use the grep plus -w (or –word-regexp) option:
$ grep -w 'fatal|error|critical' /var/log/nginx/error.log
Copy the code
Note that word characters include alphanumeric characters (such as A-z, A-z, and 0-9) and underscores (_), and all other characters are treated as non-word characters.
conclusion
In our work, we often need to use the grep command to search strings. After learning the skill of using grep to search multiple strings, we must have the opportunity to use it. In fact, grep is much more than this article. If you have any questions about grep or would like to learn more about it, let me know in the comments!