Tag Archives: Unix

Grep This!

When a problem comes along
You must grep it
Before the cream sets out too long
You must grep it
When something’s goin’ wrong
You must grep it

I say grep it
Grep it good

*My apologies to Devo.

This is another in our continuing series of articles where I try to get you to not be so fearful of the command line. Remember, the command line is your friend, like Google. So, what is grep and how do we use it, hmm? Well, grep is one of the most popular Unix/Linux commands of all time. It’s used to search for patterns in files using regular expressions. What’s a regular expression? Well, that’s just words like you see right here… Bill loves ice cream or Mary is a brunette.

When you want to search for something in a file but you can’t quite remember exactly the part your looking for, grep can help. You can search for partial phrases and wildcards too. A wildcard is usually signified by the * (asterisk) character in commands. It works with grep also. Let’s see a couple examples, shall we?

Example 1

In this example, you’re trying to find a paragraph in a large file (your Ph.D thesis on frog flatulence) about bull frogs and their ability to communicate with their flatulence. You’re pretty sure it’s in chapter 13. That’s all you can remember.

$ grep ‘bull frog’ /home/you/thesis/frog-flat/chapter-13


chapter-13:bull frogs have been known to communicate by modulating

chapter-13:while bull frogs can be muted using Beano supplements in

chapter-13: censoring bull frogs in this way has been known to reduce

As you can see, each line containing the regular expression ‘bull frog’ has been output for you by the grep command.

Of course, grep has the usual panoply of helpful options like:

-h    – if you search more than one file at a time, the results contain the name of the file from which the string was found.

-n    – precedes each line with the line number where it was found

-i    – tells grep to ignore case so that it treats “the” and “The” as the same word

-l    – displays a list of files that contain the string

-w    – restricts the search to whole words only

What if you wanted to exclude some word or phrase from your search of a certain file? Well, the versatile grep command can do that too.

Example 2

In this example, you want to do the opposite of what you did in Example 1. Instead of searching for lines within the file with the phrase ‘bull frog’, you want to exclude them.

$ grep -v ‘bull frog’ /home/you/thesis/frog-flat/chapter-13

The above command would now show you all the lines in the text file except those that have the phase ‘bull frog’ in them.

The first place to start with your grepping adventures would, of course, be its manual page.

$ man grep

Grep is another very useful tool in the command line arsenal, folks. Get out there and grep something. It won’t hurt you none.



Further reading:

Grep Man Page

Grep Tutorial

How to Use the Grep Command

Tips for Linux – Grep

What Is Grep?

Available @ O'Reilly Media, Inc.