How to Grep for Multiple Strings and Patterns
How to Grep for Multiple Strings and Patterns
We hope this post helped you to find out How to Grep for Multiple Strings and Patterns
Reading full article here https://www.mstvlife.com/how-to-grep-for-multiple-strings-and-patterns/
grep
is a robust command-line instrument that lets you searches a number of enter recordsdata for traces that match an everyday expression and writes every matching line to plain output.
On this article, we’re going to point out you tips on how to use GNU grep
to seek for a number of strings or patterns.
Grep A number of Patterns #
GNU grep
helps three common expression syntaxes, Primary, Prolonged, and Perl-compatible. When no common expression kind is specified, grep
interpret search patterns as primary common expressions.
To seek for a number of patterns, use the OR (alternation) operator.
The alternation operator |
(pipe) lets you specify completely different attainable matches that may be literal strings or expression units. This operator has the bottom priority of all common expression operators.
The syntax for looking out a number of patterns utilizing the grep
primary common expressions is as follows:
Reading full article here https://www.mstvlife.com/how-to-grep-for-multiple-strings-and-patterns/
grep 'pattern1|pattern2' file...
All the time enclose the common expression in single quotes to keep away from the interpretation and enlargement of the meta-characters by the shell.
When utilizing primary common expressions, the meta-characters are interpreted as literal characters. To maintain the particular meanings of the meta-characters, they should be escaped with a backslash (). For this reason we’re escaping the OR operator (|
) with a slash.
To interpret the sample as an prolonged common expression, invoke grep
the -E
( or --extended-regexp
) choice. When utilizing prolonged common expression, don’t escape the |
operator:
Reading full article here https://www.mstvlife.com/how-to-grep-for-multiple-strings-and-patterns/
grep -E 'pattern1|pattern2' file...
For extra details about tips on how to assemble common expressions, verify our article Grep regex.
Grep A number of Strings #
Literal strings are essentially the most primary patterns.
Within the following instance, we’re looking for all occurrences of the phrases deadly
, error
, and vital
within the Nginx log error file:
grep 'fatal|error|critical' /var/log/nginx/error.log
If the string you are searching includes spaces, enclose it in double quotation marks.
Here is the same example using the extended regular expression, which eliminates the need to escape the operator |
Reading full article here https://www.mstvlife.com/how-to-grep-for-multiple-strings-and-patterns/