How to Use Grep for Multiple Strings and Patterns

Introduction

Before we begin talking about how to use grep for multiple strings and patterns, let's briefly understand - What is grep?

grep is a command-line tool that searches one or more input files for lines that match a regular expression and prints each one to standard output.

When dealing with complex search requirements, the ability to grep for multiple strings and patterns becomes extremely useful. By understanding the syntax and techniques involved, users can efficiently search and extract specific information from files or command outputs.

In this tutorial, we will explore how to utilize grep for multiple strings and patterns, allowing you to effectively narrow down your search results. We will also address a few FAQs on how to use grep for multiple strings and patterns.

Grep Multiple Patterns

Basic, Extended, and Perl-compatible regular expression syntaxes are supported by GNU grep. grep interprets search patterns as simple regular expressions when no regular expression type is supplied.

Use the OR (alternation) operator to find multiple patterns.

The pipe operator | allows you to define many possible matches, which can be literal texts or expression sets. Of all the regular expression operators, this one has the lowest precedence.

The following is the syntax for scanning multiple patterns with the grep basic regular expressions:

grep 'pattern1\|pattern2' file...

To prevent the shell from interpreting and expanding the meta-characters, always enclose the regular expression in single quotes.

Meta-characters are treated as literal characters when employing basic regular expressions. The meta-characters must be escaped with a backslash to maintain their particular meanings (\). This is why we use a slash to escape the OR operator (|).

Invoke grep with the -E (or --extended-regexp) option to interpret the pattern as an extended regular expression. When using an extended regular expression, the | operator should not be escaped:

grep -E 'pattern1|pattern2' file...

Check out our Grep regex tutorial for more information on how to create regular expressions:

Regular Expressions in Grep (Regex)
In this tutorial, you will use regular expressions in the GNU version of grep, which comes installed by default on most Linux systems.
Regular Expression in Grep

Grep Multiple Strings

The simplest fundamental patterns are literal strings.

We're looking for all occurrences of the words fatal, error, and critical in the Nginx log error file in the following example:

grep 'fatal\|error\|critical' /var/log/nginx/error.log

Enclose the string you're looking for in double quotation marks if it contains spaces.

The extended regular expression, which avoids the requirement to escape the operator |, is used in the following example.

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

grep is case-sensitive by default. The uppercase and lowercase characters are treated separately in this scenario.

Invoke grep with the -i option (or --ignore-case) to ignore case when searching:

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

grep will display all lines where the string is embedded while searching for a string. If you search for "error," grep will also output lines when "error" is embedded in larger phrases like "errorless" or "anti-terrorists."

Use the -w (or --word-regexp) option to return just those lines where the provided string is a whole word (contained by non-word characters):

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

Alphanumeric characters (a-z, A-Z, and 0-9) and underscores (_) make up word characters. Non-word characters are all the rest of the characters.

Visit our tutorial on grep command for more information on its options:

Grep Command in Linux (Find Text in Files)
In this tutorial, you will use grep command that looks for lines that match a pattern in one or more input files and outputs

FAQ's to grep for multiple strings and patterns

Can I search for multiple patterns across multiple files simultaneously? 

Yes, you can use the grep command followed by the patterns and filenames to search for multiple patterns in multiple files.

What if I want to search for multiple patterns but exclude certain results? 

You can use the -v option with grep to exclude specific patterns from the search results, such as grep -v "pattern to exclude" file.txt.

Is it possible to grep for multiple patterns and display line numbers? 

Yes, you can use the -n option with grep to display line numbers when searching for multiple patterns.

Can I combine multiple grep commands together? 

Yes, you can use the pipe symbol | to combine multiple grep commands, allowing you to filter the output further based on different patterns.

Can I save the output of grep to a file?

 Yes, you can use the > or >> operator to redirect the output of grep to a file, such as grep "pattern" file.txt > output.txt.

Can I search for patterns in files based on certain file types only? 

Yes, you can use the --include or --include-dir options along with the -r or -R option to search for patterns in specific file types or directories only.

How do I search for patterns in the output of a command with grep? 

You can use the pipe symbol | to redirect the output of a command, and then use grep to search for patterns in that output.

How do I search for multiple strings in multiple files recursively? 

By using the -r or -R option along with the strings and directory path, you can search for multiple strings in multiple files recursively.

Conclusion

Being able to use grep for multiple strings and patterns in Linux is a valuable skill for efficient text searching and filtering. The ability to search for multiple patterns simultaneously, include or exclude specific results, perform case-insensitive searches, and search across directories and subdirectories enhances the flexibility and power of the grep command.

With the knowledge gained from this tutorial, users can narrow down their search results, extract relevant information, and perform complex searches using regular expressions and wildcards. Command combinations, such as piping and redirection, further extend the capabilities of grep for filtering and saving output.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.