How to Grep for Multiple Strings, Patterns or Words

Introduction

Grep is a powerful command line tool that can be used to search for multiple strings, patterns, or words in a file or multiple files. It is a great tool for quickly finding specific information in a large amount of data. This tutorial will provide an introduction to using grep to search for multiple strings, patterns, or words. It will cover the basics of how to use grep, as well as some more advanced techniques. By the end of this tutorial, you should have a good understanding of how to use grep to search for multiple strings, patterns, or words.

How to Grep for Multiple Strings, Patterns or Words

Grep is a powerful command-line tool that can be used to search for multiple strings, patterns, or words. To search for multiple strings, patterns, or words, you can use the -e option followed by the strings, patterns, or words you want to search for. For example, if you wanted to search for the words “hello” and “world” in a file, you could use the following command:

grep -e “hello” -e “world”

This command will search the specified file for both the words “hello” and “world”. You can also use regular expressions to search for multiple strings, patterns, or words. For example, if you wanted to search for any words that start with the letter “a”, you could use the following command:

grep -e “^a.*”

This command will search the specified file for any words that start with the letter “a”. You can also use the -f option to search for multiple strings, patterns, or words from a file. For example, if you had a file containing a list of words you wanted to search for, you could use the following command:

grep -f

This command will search the specified file for all the words in the list file.

Introduction

Grep is a powerful utility available by default on UNIX-based systems. The name stands for Global Regular Expression Print.

By using the grep command, you can customize how the tool searches for a pattern or multiple patterns in this case. You can grep multiple strings in different files and directories. The tool prints all lines that contain the words you specify as a search pattern.

In this guide, we will show you how to use grep to search multiple words or string patterns. Follow the examples in this tutorial to learn how to utilize grep most effectively.

Tutorial How to Search Multiple Strings Using grep Command

Prerequisites

  • Linux or UNIX-like system
  • Access to a terminal or command line
  • A user with permissions to access the necessary files and directories

How to Grep Multiple Patterns – Syntax

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path.

The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.

grep 'pattern1\|pattern2' fileName_or_filePath

The latest way to use grep is with the -E option. This option treats the pattern you used as an extended regular expression.

grep -E 'pattern1|pattern2' fileName_or_filePath

The deprecated version of extended grep is egrep.

egrep 'pattern1|pattern2' fileName_or_filePath

Another option is to add multiple separate patterns to the grep command.

To do so, use the -e flag and keep adding the desired number of search patterns:

grep -e pattern1 -e pattern2 fileName_or_filePath

What is the Difference Between grep, grep -E, and egrep?

The egrep command is an outdated version of extended grep. It does the same function as grep -E.

The difference between grep and extended grep is that extended grep includes meta characters that were added later.

These characters are the parenthesis (), curly brackets {}, and question mark. The pipe character | is also treated as a meta character in extended grep.

Examples of Using Grep for Multiple Strings, Patterns and Words

To make sure you understand how to use grep to search multiple strings, we suggest creating a file with some text on which we are going to try out a couple of different use cases.

In our case, we named the file sample.txt and added a few paragraphs of text. We stored the file in the directory of the test user, that is, in /home/test/sample.txt

How to Grep Multiple Patterns in a File

In the examples below, we will use grep instead of extended grep. Do not forget to use the backslash before the pipe character.

Since grep does not support the pipe symbol as the alternation operator, you need to use the escape character (backslash \) to tell the grep command to treat the pipe differently.

For example, to search for the words extra and value in the sample.txt file use this command:

grep 'extra\|value' sample.txt

The output highlights the string you wanted to grep.

Terminal output when searching two words with grep.

If the same file is in another directory, you need to navigate to that directory or use the full path of the file:

grep 'extra\|value' /home/test/Desktop/sample.txt

To search for more than two words, keep adding them in the same manner.

For example, to search for three words, add the desired string of characters followed by a backslash and pipe:

grep 'extra\|value\|service' sample.txt
Terminal output when searching three words with grep.

Let’s see how the above grep command looks when using grep -E, egrep, and grep -e:

grep -E ‘extra|value|service’ sample.txt
egrep ‘extra|value|service’ sample.txt
grep -e extra -e value -e service sample.txt

We will use grep in further examples, but you can use whichever syntax you prefer.

Search for Multiple Exact Matches in a File

If you want to find exact matches for multiple patterns, pass the -w flag to the grep command.

grep -w 'provide\|count' sample.txt

For example, the output below shows the difference between searching without -w and with it:

Terminal output when searching multiple exact matches with grep.

As you can see, the results are different. The first command shows all lines with the strings you used.

The second command shows how to grep exact matches for multiple strings. The output prints only the lines that contain the exact words.

Note: Grep offers many functionalities. Learn how to use grep for additional use cases.

Ignore Case when Using Grep for Multiple Strings

To avoid missing something when you search for multiple patterns, use the -i flag to ignore letter case.

For example, we will ignore case with this command:

grep -i 'phoenix\|linux' sample.txt
Output for grep command to ignore case while searching for multiple patterns.

The output shows how the two commands differ. If you include the -i flag and ignore letter case, the result for multiple matches includes all matches.

This way, you get additional results. If you also add the -w flag to this command, you can narrow down the results even further:

Terminal output when ignoring case while searching two exact matches with grep.

Show the Count of Multiple Matches in a File

Let’s say you are monitoring a log file, and you want to see if the number of warnings or messages increases. You don’t want to see detailed results when a large number of matches return.

For example, to show the count of multiple matches in the bootstrap.log file, enter:

grep -c 'warning\|error' /var/log/bootstrap.log
The output for the count of multiple matches for the grep command.

The output prints the number of matches. This way, you can quickly determine if the number of warnings and errors increased.

Grep for Multiple Patterns in a Specific File Type

You can use grep to search multiple strings in a certain type of file only. If you want to monitor log files in one directory or if you want to search through all text files, use an asterisk and the file extension instead of a file name.

For example, to search for warnings and errors through all .log files in the /var/log/ directory, enter:

grep 'warning\|error' /var/log/*.log

To better demonstrate how this option works, we will only show the count of matches.

Terminal output when searching for multiple patterns in a specific file type with grep.

The output shows all the files that grep searched through for the strings you used.

Note: If you get a “Permission denied” message, as we did in the example above, you need sudo privileges. To include all files, use sudo with the grep command. Enter the sudo password, and grep will search through all files.

Search Recursively for Multiple Patterns in a File

The grep command searches only in the current directory when you use the asterisk wildcard.

To include all subdirectories when searching for multiple patterns, add the -R operator to grep:

grep -R 'warning\|error' /var/log/*.log

The output will return results from all files the grep command found in the /var/log/ directory and its subdirectories.

Conclusion

In this tutorial, you learned how to use grep to search multiple words or string patterns in a file. The guide also showed you how to use extended grep.

The examples in this article help you practice how to refine your grep search. Also, check out our grep regex guide to learn more.

How to Grep for Multiple Strings, Patterns or Words

Grep is a powerful command-line tool that allows you to search for patterns or words in a file or set of files. It is a great tool for quickly finding specific information in a large set of data. Grep can also be used to search for multiple strings, patterns or words at once. This tutorial will show you how to use grep to search for multiple strings, patterns or words.

Using Grep to Search for Multiple Strings

The most basic way to use grep to search for multiple strings is to use the -e option. This option allows you to specify multiple strings to search for. For example, if you wanted to search for the words “foo” and “bar” in a file, you could use the following command:

grep -e "foo" -e "bar" filename

This command will search the file for any lines that contain either “foo” or “bar”. You can also use the -E option to search for multiple strings using regular expressions. For example, if you wanted to search for any lines that contain either “foo” or “bar”, you could use the following command:

grep -E "foo|bar" filename

Using Grep to Search for Multiple Patterns

Grep can also be used to search for multiple patterns. To do this, you can use the -f option. This option allows you to specify a file containing a list of patterns to search for. For example, if you had a file called “patterns.txt” containing the following patterns:

foo
bar
baz

You could use the following command to search for any lines containing any of these patterns:

grep -f patterns.txt filename

Using Grep to Search for Multiple Words

Grep can also be used to search for multiple words. To do this, you can use the -w option. This option allows you to specify a list of words to search for. For example, if you wanted to search for the words “foo”, “bar” and “baz”, you could use the following command:

grep -w "foo" -w "bar" -w "baz" filename

This command will search the file for any lines that contain all of the specified words. You can also use the -W option to search for multiple words using regular expressions. For example, if you wanted to search for any lines that contain all of the words “foo”, “bar” and “baz”, you could use the following command:

grep -W "foo|bar|baz" filename

Conclusion

Grep is a powerful command-line tool that can be used to search for multiple strings, patterns or words. By using the -e, -E, -f, -w and -W options, you can easily search for multiple strings, patterns or words in a file or set of files.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?