How to use the Grep command to find multiple strings
Hello everyone, I am Liang Xu!
Today we show you a very useful technique is to use grep
a command to find more strings.
A brief introduction, the grep
command can be understood as a powerful command line tool, you can use it to search for text matching regular expressions in one or more input files, and then use standard output for each matching text Write out the format.
Therefore, good promise to share with you how to use grep
a variety of search mode commands and the use of grep
search multiple strings:
Multi-mode Grep command
grep
The command supports three kinds of regular expression syntax: Basic , Extended and Perl-compatible . When the regular expression type is not specified, the grep
command defaults the search mode to 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 text strings or expression sets. It is worth noting that among all regular expression operators, this operator has the lowest priority.
Use grep
the command basic regular expression search multiple pattern matching syntax is as follows:
$ grep 'pattern1\|pattern2' filename
It should be noted here that the regular expression should always be enclosed in single quotation marks, because the content in single quotation marks is output as it is, and the content enclosed in single quotation marks will not be replaced regardless of whether it is a constant or a variable.
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 need 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 expressions, there is no need to escape the OR operator ( | ):
$ grep -E 'pattern1|pattern2' file
Grep command to search multiple strings
Generally we think that text strings are the most basic pattern.
Next, we will use an example to search 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
Also note that if the string to be searched contains spaces, you need to enclose it in double quotes.
Here is the same example using extended regular expressions, it does not require escape characters:
$ grep -E 'fatal|error|critical' /var/log/nginx/error.log
By default, grep
commands are case sensitive. To ignore case when searching, please call the grep
add -i
(or --ignore-case
) the option, for example:
$ grep -i 'fatal|error|critical' /var/log/nginx/error.log
When you want to search for a word, for example, you want to search for a word error
, grep
the command output will contain all error
lines of the string that it contains in addition to the output error
line of the word, but also contains the output errorless
or antiterrorists
other non- error
word lines, so It is extremely inconvenient.
Therefore, to return only the specified string is a line of whole-word or non-word character is enclosed by a line up, you can use the grep
plus -w
(or --word-regexp
) options:
$ grep -w 'fatal|error|critical' /var/log/nginx/error.log
It is worth noting that word characters include letters, numeric characters (such as az, aZ, and 0-9), and underscore (_ ). All other characters are considered non-word characters.
to sum up
At work, we often need to use grep
the command to the search string, learned how to use grep
search multiple strings this technique will have a chance to spend a. In fact, the grep
command much more than this paper stresses function, if you grep
function commands have any questions or would like to learn its other functions, please leave a message to tell me chanting!
Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:
Give it to everyone for free, just ask you to give me a thumbs up!
Ebook | Linux development learning roadmap
I also hope that some friends can join me to make this e-book more perfect!
Gain? I hope the old irons will have a three-strike combo so that more people can read this article
Recommended reading: