How to grep for Multiple Strings in Linux?

The grep command is used to find the information in any file or directory you are looking for. This is super effective, especially if you are simultaneously dealing with tons of different files and looking for specific information. By having this command, you will save considerable time and avoid having to perform this task manually.

This article will discuss how you can use the grep command effectively to find multiple strings. The content of this informative post is as follows:

Let’s start the guide!

How to Use grep Command for Multiple Strings?

The grep command can be used to find multiple strings or patterns in a file where you need to separate each string using a pipe operator (I) by following the general syntax below:

Syntax:

$ grep 'string_1\|string_2' filename

Here string_1 and string_2 will be two different texts that you are looking for in a specific file which is the filename in this case. Let’s discuss four of the main methods where you can use the grep command to find multiple strings in any specific file listed below: In the next section, we will discuss each of the above methods in detail.

Example 1: Find Multiple Strings While Considering the Case Sensitivity

The grep command can be used to find multiple strings that you have provided it to. For that, you need to follow the syntax mentioned below

$ grep ‘it\|you’ testfile.txt

In the same way, if you want to find more than two strings in a file, then we can do that as well using the syntax:

$ grep ‘it\|you\|and’ testfile.txt

Now you can see that it also highlights the ‘and’ string as well along with the other two strings.

Example 2: Find Multiple Strings While Ignoring Case Sensitivity

The above method highlights only those that exactly match the strings in a small letter while ignoring any word that starts with the capital letter, as shown below:

So, to ignore this case sensitivity, you can use the ‘-i’ flag by following the syntax mentioned below:

$ grep -i ‘it\|you’ testfile.txt

As you can see in the above image it is now highlighting both upper and lower-case strings that you are looking for.

Example 3: Find Exact Matching Multiple Strings in a File

There is one common issue with the above two methods that it highlights all those words which contain the strings, as you can see in the image below:

This is not always recommended as sometimes users want to highlight the exact matching string that they are looking for. So, to do that, you need to use the ‘-iw’ flag followed by the strings that you want to find as shown below:

$ grep -iw ‘it\|you’ testfile.txt

Now, you can see that it is only showing you the exact matching keywords while ignoring everything else.

Example 4: Find Multiple Strings in Multiple Files (Recursive Approach)

If you want to find any specific strings from the whole directory, then you can do that by using the ‘R’ flag. To give you an example, two more files have been created with the same text inside them, and we have placed them in the home directory. The ‘pwd’ command can help you find the complete path of your home directory.

Additionally, you will need to provide the format of the file from which you want to retrieve those strings, which is ‘.txt’, and a complete code is shown below:

$ grep -iwR ‘it\|you’ /home/itslinux/*.txt

Bonus Tip: How to Use Regular Expressions to grep for Multiple Strings?

You can also use the regular expression along with the grep command, and one of its examples is given below:

$ grep -E "(hello|world)" test

In the above image, the ‘E’ flag needs to be used when you are dealing with the regular expressions.

Conclusion

With the grep command, you can search for any information in any file or directory. The grep command can be used to find multiple strings in a single file or multiple files. This post has demonstrated various examples of the grep command to find multiple strings. For a more efficient search, we have also practiced the use of regular expressions to search for multiple strings in a file or multiple files in a directory.