Search for Multiple Patterns With grep
You can use grep tp look for multiple search terms in the same command. Here's how to do that.
Most Linux users use the grep command daily to sort specific keywords in a file. And there's nothing wrong with it.
But how would you use the grep command when you have to specify multiple patterns to get the desired output?
Well, it is pretty easy to use multiple patterns, and in this tutorial, I will share several examples of how you can use various patterns with the grep command.
How to use multiple patterns with the grep command
When you consider using multiple patterns with grep, you are given three operators: AND, OR, and NOT and each has a different meaning.
- AND (
.*
in regular expressions): It is used to search multiple patterns in a specific order and it will ensure that the given multiple patterns are in the same line. - OR (
|
in regular expressions): It is used when you want to get results from at least one of the multiple patterns and will give you output if any of the given patterns match. - NOT (
-v
): It is used when you want everything but not the output from the given pattern.
Now, let's have a look at some practical examples.
1. Find one or the other string
So let's say you have to look for one or the other (either this or that situation) and in that case, you can use the grep command in the following manner:
grep "String1\|String2" File.txt
For example, here, I want to look for the lines containing apple
or orange
in the fruits.txt
file, then I will be using the following:
grep "apple\|orange" fruits.txt
In the above snapshot, in the left window, I have used the cat command to print the file contents and in the right window, I have used the command that gave me the output with the specific requirement.
Similarly, you can use the -E
flag to avoid using \
to separate different patterns:
grep -E "String1|String2|String3" File.txt
So lets I want to look for "apple" or "pear" or "mango" from the fruits.txt
, then, I will be using the following command:
grep -E -n"apple|mango|pear" fruits.txt
Here, I've used the -n
flag which will print the number of the line from where the keyword was found.
2. Find one string followed by the other
This is one of the most useful examples, in my opinion, as I often find myself in a situation where I have to look for a string followed by the other.
Here, I will be using the -n
option which will notify the user of the line number if the grep finds the specified string:
grep "String1\(String2\)" recipes.txt
For example, here, I want to find the Apple followed by the pie in the recipes.txt
, so, I used the following command:
grep -n "Apple\( pie\)" recipes.txt
And as you can see, it gave me the output from the specified conditions and it was the first line of the file.
Similarly, you can also add options such as apple followed by pie or cider as shown in the following command:
grep -n "Apple\( pie\| cider\)" recipes.txt
If you notice closely, every option placed inside the brackets ()
starts with the space as in the file, they must be separated by the space.
But if you want the continuous string that combines one or the other option, you can remove the space.
This will instruct grep to find a string that starts immediately after the other one. So if I were to remove space from the above command, it will look for two possibilities: Applepie
or Applecider
.
3. Look for string1 or string2 is followed by string3
You may want to look for multiple strings with a condition like Sting1
or String2
must be followed by the String3
.
So in that case, you can use the grep command in the following syntax:
grep "String1\|String2" File.txt | grep "String3"
Let's say I want to look for a line that has Apple
or pie
followed by cinnamon
, then, I will be using the following:
grep "apple\|pie" recipes.txt | grep "cinnamon"
4. Use conditions after patterns
Let's say you gave instructions to match patterns in order but what if you want to add conditions in between?
Also, I will be using AND operator here to make things easy.
So let's suppose you have data of multiple users with their name, age, and email address named as data.txt
and it looks like this:
John Doe,2,[email protected]
Jane Smith,30,[email protected]
Alex Johnson,35,[email protected]
And you want to find the data of the user with an age less than 10 years, so how would you do that?
Simple, you can use a condition that will be attached to the previous condition and look for ages less than 10 years:
grep -E '.*,[0-9],' data.txt
Looks complex? Let me break it down for you.
.*
: It is used to match any character and in this case, it will print the name of the user.,
: Matches with the comma.[0-9]{1}
: The first bracket[0-9]
is a condition to look for a number that is single-digit and ranges from 0 to 9.
And you can expect the following result after the execution:
But what if you want to find users aged between 30-40?
Well, in that case, you can make a slight adjustment to the above command as shown:
grep -E '.*,3[0-9],|,40,' data.txt
Here, 3[0-9]
means the first integer of the age should be 3 and the right next it can range from 0 to 9. This means the condition is all set for 30 to 39.
And one OR operator |,40
is used to look for users having age of 40.
So if you execute the above command, you can expect the following results:
5. Use multiple condition parameters to have an exact match
Let's say you have multiple possibilities of matching strings. So what's the best way to filter out the result?
Let me give you an example first.
Suppose you have to look from String1
or String2
, and then filter the results to only show lines that also contain either String3
or String4"
.
So in that situation, you can execute the grep in the following manner:
grep -E "String1|String2" File.txt | grep -E "String3|String4"
The above command will only print the line that has a combination of at least String1 or String2 (either one of them or both) and the String3 and String4.
For example, If I want to find a line that contains Apple
or banana
or both and pie
or bread
or both, then, I will have to use the following:
grep -E -n "apple|banana" recipes.txt | grep -E "pie|bread"
6. Exclude pattern with grep
To exclude a given pattern, you'd have to use the NOT operator.
And to do so, you'd have to use the -v
flag with the grep command which will invert the output of the given patterns:
grep -v -E 'pattern1|pattern2' file.txt
For example, here, I have excluded banana
and mango
so it should print everything except those two keywords:
grep -v -E 'banana|mango' fruits.txt
Pretty cool. Isn't it?
More on grep
Here's a quick tip on using the grep command to recusrive search in all files and directories.
Here's a little grep cheat sheet to help you remember the commands.
And if you need detailed examples, this guide is for you.
I hope you will find this guide helpful. And if you have any queries, feel free to ask in the comments.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.