How to Grep for Multiple Strings, Patterns or Words

May 5, 2020

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.

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more
How to Use mkdir Command to Make or Create a Linux Directory
December 1, 2023

The mkdir command in Linux allows users to create or make new directories. mkdir stands for "make directory"...
Read more
How To Use grep Command In Linux/UNIX
February 29, 2024

This guide details the most useful grep commands for Linux / Unix systems. After going through all the...
Read more
How to Find Files in Linux With the Find Command
December 19, 2018

The find command is a useful command-line tool in Linux. It allows you to use the terminal to search...
Read more