Grep is a strong tool that comes with UNIX-based systems by default. How to Grep for Multiple Strings, Patterns or Words? Global Regular Expression Print is the name.
You can control how the program looks for a pattern, or multiple patterns in this example, by using the grep command. Multiple strings can be found with grep in various files and locations. The search tool prints all lines that match the search pattern you enter.
The usage of grep to search for multiple words or string patterns is demonstrated in this guide. To understand how to use grep most efficiently, pay attention to the examples in this article.
- UNIX-like or Linux-based system
- Access to a command line or terminal
- A user with authorization to access the required directories and files
How to Grep Multiple Patterns – Syntax
When looking for numerous patterns in a file, the basic grep syntax entails using the grep command, strings, and the file’s name or path.
The patterns must be separated by the pipe symbol and enclosed in single quotes. To use regular expressions, place a backslash before pipe |.
fileName or filePath grep 'pattern1|pattern2'
The -E option is the most recent way to use grep. With this selection, the pattern you selected is treated as an extended regular expression.
grep -E "fileName or filePath" "pattern1|pattern2"
Extended grep’s obsolete counterpart is egrep.
fileName or filePath egrep 'pattern1|pattern2'
Another choice is to use the grep command with numerous independent patterns.
How to Grep for Multiple Strings, Patterns, or Words? Use the -e flag and keep adding the required amount of search patterns to do this:
pattern1 and pattern2 in grep fileName or filePath
What is the Difference Between grep, grep -E, and egrep?
Extended grep has been superseded by the outmoded grep command. It performs the same task as grep -E.
Extended grep contains meta characters that were added later compared to regular grep, which does not.
The question mark, curly brackets, and parenthesis are these symbols. In extended grep, the pipe character | is also considered as a metacharacter.
Examples of Multiple String, Pattern, and Word Grep Searches
We advise generating a file with some text on which we will test out a few different use cases in order to ensure that you comprehend how to use grep to search multiple strings.
In our situation, we gave the file the name sample.txt and then added some content. We kept the file in the test user’s home directory, at /home/test/sample.txt.
How to Grep Multiple Patterns in a File
Instead of extended grep, we’ll use grep in the examples that follow. The backslash must be used before the pipe character.
You must use the escape character (backslash) to instruct the grep command to handle the pipe differently because grep does not allow the pipe symbol as the alternation operator.
Use this command, for instance, to look for the words “extra” and “value” in the sample.txt file:
txt grep "extra value"
The string you wanted to grep is highlighted in the output.
If the same file is located in a different directory, you must either go there or use the complete path to the file:
/home/test/Desktop/sample.txt grep "extra value"
How to Grep for Multiple Strings, Patterns or Words? Continue adding terms in this way to search for more than two.
Add the desired string of letters followed by a backslash and a pipe to find three words, for instance:
grep sample.txt 'additional value service'
Here is how the grep command above appears when run with the grep -E, egrep, and grep -e options:
grep -E extra, value, and service sample.txt
egrep "additional|value|service" sample.txt
txt grep -e extra -e value -e service
In subsequent examples, we’ll use grep, but you are free to use whichever syntax you like.
Find Several Exact Matches in a File
Use the -w argument with the grep program to locate exact matches for multiple patterns:
txt grep -w "provide count"
For instance, the result below illustrates the distinction between searching with and without the -w option:
The outcomes differ, as you can see. The first command displays every line containing the strings you entered.
The second command demonstrates how to find exact matches across many strings using grep. Only the lines with the exact wording are printed in the output.
When using Grep for several strings, ignoring the case
Use the -i switch to ignore letter case while searching for multiple patterns to prevent missing anything.
For illustration, the following command will disregard the case:
txt grep -i "Arzhost|linux"
The results demonstrate how the two commands are different. When there are numerous matches, all of the matches are returned if the -I flag is used and the letter case is ignored.
How to Grep for Multiple Strings, Patterns, or Words? You achieve more outcomes in this way. This command’s -w parameter allows you to even more precisely limit the results:
The number of multiple matches in a file is displayed.
Let’s imagine you are keeping an eye on a log file to check if there are more warnings or notifications. When several matches return, you don’t want to see specific results.
For instance, type the following to display the number of multiple matches in the bootstrap.log file:
'warning error' /var/log/bootstrap.log grep -c
The number of matches is displayed in the output. This allows you to rapidly ascertain whether there were more warnings and faults.
Grep for Multiple Patterns in a Specific File Type
Grep can be used to search numerous strings within a specific kind of file. Use an asterisk and the file extension in place of a file name if you want to monitor log files in a single directory or search through all text files.
For instance, to search through all.log files in the /var/log/ directory for errors and warnings, type:
/var/log/*.log grep "warning error"
We will only display the number of matches in order to better illustrate how this option functions.
How to Grep for Multiple Strings, Patterns or Words? The output lists every file that greps browsed for the strings you specified.
Search Recursively for Multiple Patterns in a File
When you use the asterisk wildcard, the grep command only looks in the current directory.
Add the -R operator to grep to include all subdirectories when looking for multiple patterns:
grep -R /var/log/*.log "warning error"
All files that the grep command discovered in the /var/log/ directory and its subdirectories will be listed in the output.
Conclusion
Have you discovered How to Grep for Multiple Strings, Patterns, or Words? or string patterns with grep in this tutorial. You learned how to use extended grep from the guide as well.
You can improve your grep search by practicing with the examples in this article. For additional information, see our grep regex guide.