Looking for Something? How to grep Multiple Strings in Linux

The Linux terminal is filled with helpful instructions, however few are as highly effective because the seemingly easy grep. It stands for World Common Expression Print, printing the outcomes of user-defined system searches for collections of characters.

grep is extraordinarily highly effective however may be fairly intimidating to make use of, so as we speak, you will be taught some fundamentals. If you’re searching for some data inside the paperwork in your machine, often, you will be searching for a number of phrases directly.

This text focuses on easy methods to search a number of strings utilizing grep and can present you just a few related ideas and methods for utilizing grep typically.


Looking out A number of Strings in grep

Earlier than getting began, you will must ensure you are accustomed to just a few Linux fundamentals. First, you will want to have the ability to deliver up a terminal—on most programs, you do that with the important thing mixture Ctrl + Alt + T, or through the functions menu.

You will additionally must be accustomed to easy methods to reference information by path utilizing the terminal. For instance, in case you needed to enter the trail for a textual content doc positioned in a folder known as work contained in the dwelling folder, you’d reference it utilizing this format:

~/work/textdocument.txt

This syntax, utilizing the ~ and /, adopted by the situation of the file within the dwelling folder, is what you will use to indicate the grep command which information to look.

MAKEUSEOF VIDEO OF THE DAY

As an instance, for instance, you needed to search out each incidence of the phrases bee and Vanessa in a textual content doc containing the script from the 2007 Bee film. The syntax appears like this:

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

If you run this command, you will get again a listing of each line within the doc containing the phrase bee or the phrase Vanessa.


grep command search

Let’s break that command down a bit of:

grep -E

The primary half runs the grep command with the -E possibility. This directs grep to look utilizing an Extended common expression. Several types of common expressions are a topic for an additional day—and mastering common expressions takes a lifetime—however for the needs of this tutorial, the bold-E command lets you search a number of strings utilizing grep.


grep -E 'bee|Vanessa'

The subsequent half comprises the strings you need to seek for. They have to be contained in single quotes, with a pipe between every phrase.

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

The final a part of the grep name takes the trail to the file you want to search; on this case, the script to the 2007 Bee movie.

grep Ideas and Tips

This can be a nice begin, however there are many sensible methods to make use of grep. Now that you know the way to search out a number of strings, listed here are just a few instructions that may take that energy to the following stage:

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

That is similar to the earlier command, besides that it returns all strains containing bee and Vanessa from each the Bee film script and from my very own Bee story textual content file.


grep searching multiple strings

All these mentions of bees are getting fairly onerous to learn, although.

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

This takes the identical grep command and pipes it into much less. This opens the leads to an easy-to-navigate format, permitting you to scroll up and down utilizing the j and ok keys and search inside the outcomes utilizing the / key.

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

Very like the much less command, the aforementioned command takes the grep outcomes and saves all of them to a separate file. You’ll be able to then open this file in your favourite textual content editor for additional enhancing.


grep output

Associated: The Linux Instructions Reference Cheat Sheet

Working With grep in Linux

Hopefully, you’re getting an thought of simply how highly effective grep may be—and this is not even scratching the floor. Go forth, and grep!


Magnifying glass on book
How Grep Bought Its Identify? The Historical past Behind Grep’s Creation

Have you ever ever questioned what Grep stands for? Here is the true story behind the creation of Grep.

Learn Subsequent


About The Writer

Leave a Comment