Examples on How to Grep for Multiple Patterns
Sometimes, you may want to customize how you want to search for a given item in a file. Luckily, grep makes it possible for a user to grep multiple patterns by printing all lines that match your search patterns.
The syntax to use with grep when you want to search for multiple patterns is as follows:
The different patterns that you specify must be enclosed in single quotes, and we add the pipe symbol to separate them. The backslash in this case is added to symbolize that we are dealing with a regular expression. We will see a better approach to use later in this post. For now, let’s discuss the different examples to understand how to grep for multiple patterns.
Example 1: Grep Multiple Patterns
When you want to grep multiple patterns, specify the pattern, options, and the path or filename. For our first example, we are working on a file that is in the current directory. Our search patterns are “will” and “linux”. Note that when you run the following command, it is case-sensitive. Moreover, it doesn’t get the exact word. Instead, it returns any word that has the search pattern.
When you have the path to a target file, you can type the path instead of its name. This option is the most common instead of having to first navigate to the directory that contains your target file. Replace the filename with the path and execute the command as shown in the following:
Example 2: Grep Extended Regular Expressions
The old way of using grep for multiple patterns requires you to add a backslash to indicate that we are working with a regular expression. However, the latest way allows the users to add the -E option which indicates that the pattern that you are using is an extended regular expression, hence eliminating the need for a backslash.
For instance, the earlier command could be rewritten as follows:
We still get the same results.
Example 3: Grep Exact Matches
From the previous examples, we’ve seen that the “grep” command finds all patterns that have the search pattern. However, you may get a case where you want to only get the exact matches. For instance, in the word “linuxhint”, if your search pattern is “linux”, you will still get “linuxhint” in the search result.
However, adding the -w restricts the output only to contain the specific matches. Let’s rerun our previous command and see what output we get:
For the previous output, we only retrieved the exact matches, narrowing the output.
Example 4: Working with Cases
Grep is case-sensitive. However, when you add the -i flag, it ignores the case sensitivity and outputs all matches for your multiple patterns.
The following output shows how adding the -i yields different outputs without considering the cases:
Example 5: Combing Different Options
When greping multiple patterns, you can combine different options to get a specific output. For instance, let’s say you want to find the exact matches without considering a case sensitivity. For that, you can combine the -iw option as shown in the following:
Example 6: Grep Show Count
Sometimes, you may want to count the different multiple matches for your pattern. For instance, if you are working with a log file, this option lets you keep a tab of the number of times that a given entry has appeared in the target file. To show this count, use the -c option as shown in the following example:
The output is the count of the multiple matches that are present in the file. Suppose you get more counts for the particular pattern. When you rerun the command, it shows a different count.
This post discussed how you can use the “grep” command to work with multiple patterns. We elaborated the different examples on how the grep for multiple patterns works. With these examples, we hope that you can now work with multiple patterns.