Looking for Something? How to grep Multiple Strings in Linux

Looking for Something? How to grep Multiple Strings in Linux

The Linux terminal is full of useful commands, but few are as powerful as the seemingly simple grep. It stands for Global Regular Expression Print, printing the results of user-defined system searches for collections of characters.

grep is extremely powerful but can be quite intimidating to use, so today, you’ll learn some basics. If you are looking for some information within the documents on your machine, usually, you’ll be looking for several words at once.

This article focuses on how to search multiple strings using grep and will show you a few similar tips and tricks for using grep in general.


Searching Multiple Strings in grep

Before getting started, you’ll need to make sure you are familiar with a few Linux basics. First, you’ll need to be able to bring up a terminal—on most systems, you do this with the key combination Ctrl + Alt + T, or via the applications menu.

You’ll also need to be familiar with how to reference files by path using the terminal. For example, if you wanted to enter the path for a text document located in a folder called work inside the home folder, you’d reference it using this format:

~/work/textdocument.txt

This syntax, using the ~ and /, followed by the location of the file in the home folder, is what you’ll use to show the grep command which files to search.

MAKEUSEOF VIDEO OF THE DAY

Let’s say, for example, you wanted to find every occurrence of the words bee and Vanessa in a text document containing the script from the 2007 Bee movie. The syntax looks like this:

grep -E 'bee|Vanessa' ~/work/beescript.txt

When you run this command, you’ll get back a list of every line in the document containing the word bee or the word Vanessa.


grep command search

Let’s break that command down a little:

grep -E

The first part runs the grep command with the -E option. This directs grep to search using an Extended regular expression. Different types of regular expressions are a subject for another day—and mastering regular expressions takes a lifetime—but for the purposes of this tutorial, the bold-E command allows you to search multiple strings using grep.


grep -E 'bee|Vanessa'

The next part contains the strings you want to search for. They must be contained in single quotes, with a pipe between each word.

grep -E 'bee|Vanessa' ~/work/beescript.txt

The last part of the grep call takes the path to the file you wish to search; in this case, the script to the 2007 Bee film.

grep Tips and Tricks

This is a great start, but there are lots of practical ways to use grep. Now that you know how to find multiple strings, here are a few commands that can take that power to the next level:

grep -E 'bee|Vanessa' ~/work/beescript.txt ~/fun/mybeestory.txt

This is very similar to the previous command, except that it returns all lines containing bee and Vanessa from both the Bee movie script and from my own Bee story text file.


grep searching multiple strings

All these mentions of bees are getting quite hard to read, though.

grep -E 'bee|Vanessa' ~/work/beescript.txt ~/fun/mybeestory.txt | less

This takes the same grep command and pipes it into less. This opens the results in an easy-to-navigate format, allowing you to scroll up and down using the j and k keys and search within the results using the / key.

grep -E 'bee|Vanessa' ~/work/beescript.txt ~/fun/mybeestory.txt > beeresults.txt

Much like the less command, the aforementioned command takes the grep results and saves them all to a separate file. You can then open this file in your favorite text editor for further editing.


grep output

Related: The Linux Commands Reference Cheat Sheet

Working With grep in Linux

Hopefully, you are getting an idea of just how powerful grep can be—and this isn’t even scratching the surface. Go forth, and grep!


Magnifying glass on book
How Grep Got Its Name? The History Behind Grep’s Creation

Have you ever wondered what Grep stands for? Here’s the true story behind the creation of Grep.

Read Next


About The Author

Original Link

Leave a Comment

Your email address will not be published. Required fields are marked *