How to Grep for Multiple Strings and Patterns

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

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:

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:

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 deadlyerror, 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 |

grep -E 'fatal|error|critical' /var/log/nginx/error.log

By default, grep is case sensitive. This means that the uppercase and lowercase characters are treated as distinct.

To ignore case when searching, invoke grep with the -i option (or --ignore-case):

grep -i 'fatal|error|critical' /var/log/nginx/error.log

When looking for a string, grep will show all traces the place the string is embedded in bigger strings. So if you happen to had been looking for “error”, grep may even print the traces the place “error” is embedded in bigger phrases, reminiscent of “errorless” or “antiterrorists”.

To return solely these traces the place the required string is a complete phrase (enclosed by non-word characters), use the -w ( or --word-regexp) choice:

grep -w 'fatal|error|critical' /var/log/nginx/error.log

Phrase characters embrace alphanumeric characters (a-z, A-Z, and 0-9) and underscores (_). All different characters are thought-about as non-word characters.

For extra particulars about grep choices, go to our article Grep command.

Conclusion #

We have now proven you tips on how to grep to go looking a number of patterns, strings, and phrases.

We hope the How to Grep for Multiple Strings and Patterns help you. If you have any query regarding How to Grep for Multiple Strings and Patterns drop a comment below and we will get back to you at the earliest.

We hope this post helped you to find out  How to Grep for Multiple Strings and Patterns  . You may also want to see – Kill Command in Linux

Share via
Copy link
Powered by Social Snap