Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Don't Be Afraid Of Grep

TwitterFacebookRedditLinkedInHacker News

One of the most powerful commands that you could learn, grep is essentially a robust search tool. It gets its name from the acronym it stands for which is global regular expression print. This is just a fancy way of saying “to search for specific strings or patterns (using regular expressions) within a specific file or directory and print the matching results”.

To get a quick glimpse of how it works, take the most basic example:

grep donuts

This will search all the files in the current directory you’re in for any instances of the word “donuts”. When it finds a match, it will print it out to the console. Pretty freakin’ cool.

The basic structure of a grep command works like this:

grep {options} {pattern to match} {file or directory to search}

As you can see, our previous example didn’t use the options argument nor did we pass it a specific file/directory to search. These options, however, are what make the grep command so powerful!

Let’s expand our previous example a bit. Instead of searching all files within the current directory we’re in, let’s say we only wanted to search a specific file. We can pass the name of that file like so:

grep donuts desserts.txt

This will look for a file named “desserts.txt” in your current directory and only search that file for any instance of the word “donuts”. As before, it will print out all matching results to the console!

Since this last argument is essentially a file path, we can also use it to search entire directories! So to search a specific directory for any instance of “donuts”, we’d simply change the last argument like so:

grep donuts \path\to\target\directory

Assuming “directory” in the above example is a folder, grep will search its entire contents for instances of “donuts”. So many use cases!

Finally, if we wanted to search our current directory as well as all of its subdirectories, we can use a dedicated recursive option:

grep -r donuts *

This literally says, “For every match to ‘donuts’ in the current directory or in any of its subdirectories, print the matching results to the console”. Nifty.

Now, all of the previous examples are great if you just need to search for the word “donuts”. However, if you need something a little more robust, take a look at the next few examples!

First things first, if your search pattern is two words, you’ll need to place them within single quotes. This is so the grep command doesn’t mistake your second word as an invalid argument. So if you wanted to search for specific kinds of donuts, say “glazed”, you’d change the pattern argument like so:

grep ‘glazed donuts’ desserts.txt

And as you’d expect, it’ll search for “glazed donuts” in the desserts.txt file.

There are other cool things you can do with your search pattern. I mean, grep itself says you can use regular expressions. So let’s use them! Here are the most used special characters in rapid fire succession:

To search for a pattern at the beginning of a line, use the ^ character:

grep ^choc desserts.txt

This returns results like “chocolate”, “chocoholic”, and “chock-full-of-chips”. You get the idea.

Searching for a pattern at the end of a line uses the $ character:

grep ix$ desserts.txt

This will return any results with “ix” at the end. Think “twix”, “mix”, “stix”.

And finally, to search for any character, use the . character:

grep .fin desserts.txt

This will look for any results that have “fin” within them. So “muffin”, “ladyfingers”, and “fingerling” would all be valid results.

Got all that?

Everything we’ve learned so far should cover most use cases you’ll need for grep. To be thorough, though, I’ll show you a few more options that may come in handy!

By default, grep is case sensitive. If you need to ignore case, use the dedicated option -i for it:

grep -i donuts \path\to\desserts

This should get you lower and uppercase donuts ;)

A few other options are the -A and -B flags. If you ever need to return both the matching result and a specified number of lines before or after it, these are your flags! A typical use case I use these flags for is when I need to pull up the contents of some highly nested function. Let’s say I wanted to double-check the rounding logic I was using for a certain function in my utilities class. I could search for that specific function and use the -A flag to bring back the 5 lines after it like so:

grep -A 5 "FormatPercent" .\utilities\formatting.ts

This grep command literally says, “For every match to ‘FormatPercent’ in my formatting.ts file, return the match including the 5 lines that come after it. Pretty sweet. And as you can probably guess, the -B flag denotes lines before the match!

But wait! What if you need to print the lines before AND after the match? There’s a dedicated option for that! It’s the -C flag and works similarly to the -A and -B flags:

grep -C 3 “Format” .\utilities\formatting.ts

This will search my “formatting.ts” file and return any matches to “Format” along with the the three lines before and after each match!

There’s so much more you can do with grep, but these highly used options and use cases should provide a great start to your encounters with the grep command!

Thanks for reading! For more from me, follow my social accounts:

Twitter: https://twitter.com/AdrienneTacke

Instagram: https://www.instagram.com/adriennetacke/

Website: https://www.adrienne.io

Adrienne Braganza Tacke

Adrienne Braganza Tacke

Adrienne Braganza Tacke is a Filipina software engineer in Las Vegas. She is also the author of the newly released book Coding for Kids: Python. She enjoys leading by example and sharing her knowledge to help new devs become great ones.