How to grep multiple strings or patterns in Linux

grep is a powerful tool that lets you search for strings, patterns, and words in files and text outputs from commands. It’s a great way to customize your search depending on your needs. Grep also offers a variety of options to make your search more efficient.

The name, grep, derives from the command used to perform a similar operation, using the Unix/Linux text editor ed: g/re/p

There are a variety of grep utilities that can help you search through your files, including grep, egrep, and fgrep. Out of these, fgrep is often the best option due to its speed and its focus on strings and words. However, if you prefer, you can also type grep to get the same results. Ultimately, it’s up to you which of these utilities you choose to use.

In this article, we will show you how to use grep to search multiple strings and patterns at once in Linux.

Grep Multiple Strings

If you want to search multiple patterns or strings in a particular file, use the grep functionality to sort within a file with the help of more than one input word in the command.

You can use the | symbol to grep multiple strings or patterns. grep use | symbol to separate two patterns in a command.

But before you actually use it, remember to escape it so that grep doesn’t mistakenly recognize it as part of the pattern. In other words, you have to use \| instead of | in a grep command.

sudo grep [options] 'pattern1\|pattern2\|...' /path/to/file
Code language: JavaScript (javascript)

What we’ve just did is asking grep to look for multiple patterns (pattern1, pattern2, …) in a file.

Alternatively, we can pass (pipe) the output of another command to grep to search it for required strings by running the following commands.

command | grep [options] 'pattern1\|pattern2\|...'
Code language: JavaScript (javascript)

In both cases, grep will output all the lines that might contain any of the listed patterns.

Search Multiple Strings in a file

Suppose you want to search for multiple strings in a single file located in /home/nl/test.txt, and you want to do it in a single run, use the command below.

sudo grep 'string1\|string2\|string3' /home/nl/test.txt
Code language: JavaScript (javascript)

Alternatively, you can navigate into /home/nl and do the grep search. In this case, you can use only the filename in the command as you’re already in that directory.

sudo grep 'string1\|string2\|string3' /home/nl/test.txt
Code language: JavaScript (javascript)

The syntax above only allows searching for raw strings. If you want to search for regex patterns, use -E or -e option. See the examples below to see what I mean.

sudo grep -E 'string1\|string2\|string3' /home/nl/test.txt # OR sudo grep -e string1 -e string2 -e string3 /home/nl/test.txt
Code language: PHP (php)

Grep Exact Matches

If you want to find an exact match in a file, you can use the –w flag to sort the results before output. - means grep will only match the whole words.

For example, consider searching without –w, this command will bring all results that matches one of the given words. But when you use -w flag, searching will be limited as input words only match the first string. The second word is not highlighted because “–w” allows accurate matching with the pattern. Let’s see and example to see what it really means:

Grep Exact Match from a file

The first command brings all related data together in one place, while the second command helps you find exactly what you’re looking for with pinpoint accuracy.

Grep Both Uppercase and Lowercase

If you want to ignore case while searching for multiple strings and patterns, use -i option.

sudo grep -i 'string1\|string2\|string3' /home/nl/test.txt
Code language: JavaScript (javascript)

Count Occurences

Sometimes, you would want to get the number of occurences of your search string in a file, use -c (count) option. For instance, if you want to know about the errors occurring in the system. The detail is recorded in the logs file. Knowing that how many times an error has happened would be very useful.

sudo grep -c 'string1\|string2\|string3' /home/nl/test.txt
Code language: JavaScript (javascript)

Grep in multiple files

You can use wildcard characters to search for specific file types, such as *.txt files, to find multiple strings in multiple files.

sudo grep 'string1\|string2\|string3' /home/nl/*.txt
Code language: JavaScript (javascript)

The above command will only look for specified search strings in all .txt files in /home/nl directory but not in any of its subdirectories. If you also want to look for multiple strings recursively in subdirectories, use -R flag in your command.

sudo grep -R 'string1\|string2\|string3' /home/nl/*.txt
Code language: JavaScript (javascript)

This command will help you find your search strings in .txt files located in all subfolders under the /home/nl directory.

We hope that the information above helped you learn how to use grep to search for multiple patterns. We’ve also covered other software installation for Linux, such as How to install CMakeAirflowCura and ADB/fastboot on CentOS, in case you’re interested. If you have any suggestion, please feel free to leave a comment below.

Click to rate this post!
[Total: 0 Average: 0]

Leave a Comment