In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Grep command to find multiple strings". In daily operation, I believe many people have doubts about how to use Grep command to find multiple strings. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use Grep commands to find multiple strings". Next, please follow the editor to study!
Multimode 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 pattern to the basic regular expression.
To search for multiple matching patterns, you can use the OR (alternation) operator. We can use the OR operator | (pipe) to specify different matches, which can be either text strings or sets of expressions. It is worth noting that of all regular expression operators, this operator has the lowest precedence.
The syntax to search for multiple matching patterns using the grep command basic regular expression is as follows:
$grep 'pattern1\ | pattern2' filename
It is important to note that regular expressions are always enclosed in single quotation marks, because the contents in single quotation marks are output as is, and the contents enclosed in single quotation marks will not be replaced by constants or variables.
When using basic regular expressions, metacharacters are interpreted as literal characters. To retain the special meaning of metacharacters, they must be escaped with a backslash (\). This is why we 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 expressions, you do not need to escape the OR operator (|):
$grep-E 'pattern1 | pattern2' file
The Grep command searches for multiple strings
Generally speaking, we think that the text string is the most basic pattern.
Next we will give an example of searching for all fatal, error, and critical strings that appear in a user's log error file. The syntax is as follows:
$grep 'fatal\ | error\ | critical' / var/log/nginx/error.log
It is also important to note that if the string you are searching for contains spaces, you need to enclose it in double quotes.
The following is the same example of using an extended regular expression, which does not require escape characters:
$grep-E 'fatal | error | critical' / var/log/nginx/error.log
By default, the grep command is case sensitive. To ignore case when searching, call the grep plus-I (or-- ignore-case) option, as shown in the following example:
$grep-I 'fatal | error | critical' / var/log/nginx/error.log
When you only want to search for a word, for example, you want to search for the word error, the grep command will output all lines containing error strings, that is, it will output not only lines containing error words, but also lines containing non-error words such as errorless or antiterrorists, which is extremely inconvenient.
So to return only lines where the specified string is the whole word, or that is enclosed by non-word characters, you can use the grep plus-w (or-- word-regexp) option:
$grep-w'fatal | error | critical' / var/log/nginx/error.log
It is worth noting that word characters include alphabetic, numeric characters (such as amurz, amelz, and 0-9) and underscores (_), and all other characters are considered non-word characters.
At this point, the study on "how to use the Grep command to find multiple strings" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.