Connect with us

Website Tutorials

How to Grep Multiple Strings

Published

on

How to Grep Multiple Strings

Grep is one of the most common Linux commands. Earlier, I’d written an article on using the pipe command, and grep was the most important example of using the pipe operator to filter the output of commands and search for strings. In this tutorial, I’ll show you how to use grep to search for multiple string patterns instead of just one. In addition, you’ll see how to match multiple strings using either the “OR” or “AND” logic. You’ll also see different ways of accomplishing the same thing and improving search efficiency.

We’ll also explore some pitfalls, and whether you might be better off using another command like awk or sed. I’ll also show you how to modify grep so that it shows not just the matched line, but also the ones above and below it, and how to search recursively through subdirectories.

Using Basic Grep for Pattern Matching

Grep is the “go-to” tool for matching string patterns in Linux. Here’s a quick example of how to use it. Let’s say you have a file “example.txt”, and you want to search for all lines containing the word “apple”. Here’s what you would use:

grep "apple" example.txt

The syntax is easy. “apple” is the string pattern, and “example.txt” is the file you want to search. When grep finds a line containing “apple”, it’ll print out as shown here:

Grep Apple
Grep Apple

The above file contains two lines with the word “apple”. As you can see, grep operates on a line-by-line basis, and not on a “per file” basis. This is because it’s frequently used to search structured text outputs like those from a command, or a log file.

Using Grep to Search for Multiple Strings with “OR”

In the above example, we searched for just one string in the file – “apple”. However, if you want to search for multiple strings, you can do this. Let’s say I want to find the words:

Advertisement
  1. “not” OR
  2. “apple”

Then the following command would work:

grep -e "not" -e "apple" filename

You can see that the above command now includes two lines:

Not OR Apple
Not OR Apple

Once again, note that the grep command processes its input line-by-line, and returns those lines that contain either one string or the other.

The “-e” flag stands for “extended”. Extended grep allows you to match multiple strings at the same time, as shown above. You just need to add an additional “-e” flag for each pattern you need to find.

Using “-E” for Efficiency

Creating a separate “-e” tag can be a drag if you have many patterns you want to match. To deal with this problem, you can use the “-E” flag (with a capital “E”). This way, you can grep multiple strings like this:

grep -E "not|apple" example.txt

This command is the same as the previous one, but as you can see, it’s a bit easier to type, particularly when the number of strings is large.

Using egrep

For those who want to cut down the command size even further, there’s a convenient function called “egrep”, which is nothing but “grep” with the automatic inclusion of the “-E” extended flag. Here’s what it looks like in action:

Advertisement
egrep "not|apple" example.txt

This might look like splitting hairs, but when you’re constantly performing string actions, command efficiency and readability matter.

Matching Multiple Strings with the “AND” Operator

The above examples have all been for using grep with multiple strings on an “OR” basis, meaning that they need to match at least one of the patterns. However, sometimes you need to use the “AND” operator, where grep finds all the patterns in a file. Here’s how you do it.

Let’s say you want to find all files in a text file that contain the words:

  1. This
  2. is

The standard way to do this would be to “pipe” the command from one grep command into the other. For example, here’s what the command to find the above two words would look like:

grep "This" example.txt | grep "is"

Here’s the output of the above command with the “example.txt” file we created earlier:

This and IS
This and IS

As you can see the grep command is case-sensitive, and it leaves out the third line, which contains “this is” instead of “This is”. So it only highlights the second and third lines. If you want grip to be case insensitive, you need to add the “-i” flag, and then it will show you the last line as well:

grep -i "This" example.txt | grep "is"

You can chain together any number of grep statements like this, and each would filter the output of the other, creating an “AND” chain.

Advertisement

But grip isn’t the only command you can use to search for multiple strings. You can also use awk and sed, as shown below.

Using “awk” to Find Multiple Strings

Instead of using piped command with grep as above, you can use other tools like “awk”. You might find awk easier to use, as it’s more intuitive to simply provide the multiple strings to search for at once, rather than pipe together commands. In addition, depending on the complexity of the task, awk can be much more efficient.

For example, to perform the same operation as above using “awk” instead of “grep”, you would use:

awk '/apple/ && /This/' example.txt

Here’s what it looks like:

Using Awk for Multiple Strings
Using awk for Multiple Strings

As you can see, the awk command is much more terse than the grep command – though arguably more complex in syntax, because of the special symbols (/) and (&). But if you have a lot of strings that you need to find simultaneously, then awk is not only more elegant but also more efficient than grep.

One disadvantage, as the screenshot shows, is that awk doesn’t highlight the matches in a special color as grep does. But if you’re searching for multiple strings – either using “OR” or “AND”, the highlights don’t make too much sense, anyway.

Advertisement

Using the “sed” Command

Another command we can use to find multiple files is “sed”. This is a tool for editing streams of text, but it’s less powerful than awk and the syntax is more awkward as well. Here’s how to find multiple patterns of text at the same time using sed:

sed -n '/apple/{/This/p}' example.txt

The above syntax first filters for “apple”, and then filters for “This”. Like I said, it’s messy, but it does the job. Here’s a screenshot:

Sed
Sed

As with awk, sed doesn’t highlight string patterns either.

Conclusion

As you can see, there are many ways to match patterns, but using grep might end up being the simplest of them all for most cases. It’s a syntax that everyone already knows, and chaining together grip statements comes naturally to people who are used to how Linux works. So whether you’re searching for “OR” matching or “AND” matching grep is your go-to tool.

Only if your needs are beyond the ordinary, should you look to tools like awk and sed. For example, if you want to match multiple regular expressions stored in a file, then awk is a better tool than grep. Or if you’re concerned about efficiency, where you’re searching for thousands of patterns across a large number of files and directories. Otherwise, just stick with grep!

Advertisement

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.