Tag Archives: grep

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

Results:

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.

Later…

~Eric


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.

Advertisements

My 2011 Linux Resolutions

2010 is shot in the arse. 2011 is right around the corner. Here’s what I resolve to do Linux-wise for the new year:

  • I want to try to continue to give back to the Linux community the knowledge and insights that I have garnered while interacting within that community for the past 4+ years.

Linux should be all about community. While there are many friendly MS Windows and Apple/Mac support forums and communities, it’s doubly important for Open Source and GNU/Linux to remember that much of its success and evolution is driven by its faithful community. We should all resolve to be more helpful in 2011; and not just regarding Linux.

  • I want to learn to use the grep command more efficiently.

Grep is a command line tool that can be used to find lines in a file using regular expressions. I confess that most of the time when I use the grep command it’s because I’m copying someone else’s recommended command into my terminal. I want to learn more about how the command works and where best to use it.

  • I want to learn a bit about shell scripting, particularly BASH shell scripting.

Shell scripts are little programs that can be written to perform a series of commands within the shell. They can be quite useful. I’m interested in learning the basics of BASH shell scripting.

  • I want to install and experiment with at least one distribution that I’ve never installed before.

I’m thinking of maybe Frugalware. We’ll see.

  • Lastly, I would like to consider attaining a Linux IT certification of some sort.

This may be a bit much to chew, but I can dream, can’t I? HERE is a Wikipedia article about the Linux Professional Institute certifications.

That about covers my Linux resolutions for 2011. I have them here in print to remind me from time to time how I’m progressing.

Later…

~Eric

Image credits: pre-altered reader courtesy of clipartheaven.com

Further reading:

How to Use the Grep Command In Linux/Unix

Linux certification is gaining appeal among administrators

Linux Professional Institute (Certifications)