Saturday, June 23, 2012

Using the Grep Command



The grep command is a simple Linux command line utility used for locating strings of text in files. Here we're going to walk through a brief tutorial on using the grep command.

Grepping a single file

We're going to assume that we have a single file labeled "declaration" that contains part of the Declaration of Independence. Let's say we're looking with anything in the file "Declaration" with the word "lives" in it. The syntax for the command would be as follows.

grep "lives" declaration

Essentially what this command does is says "look for the word 'lives' in the file 'declaration'". Interestingly enough, the grep command will actually display the entire line the word was found in, so running this command gives us the following:

"He has plundered our seas, ravaged our Coasts, burnt our towns, and destroyed the lives of our people."

Grepping a directory

OK, so let's say that you know some of the text in a file but you're not sure exactly where that file is. Finding that file is one of the most basic applications of the grep command. To grep for a string of text in a directory, use the following:

grep "lives"./*

The * acts as a wildcard symbol in most Linux and Unix operating systems and basically just tells the grep command to search "All". In addition to just finding the text, the grep command, in this instance, will also show you the file it found your string of text in. The output of the command above is as follows:

"./declaration:He has plundered our seas, ravaged our Coasts, burnt our towns, and destroyed the lives of our people."

Grepping Recursively

In certain cases we may not have the luxury of knowing which directory a file is found in. In this case, the ability to grep recursively becomes useful. Grepping recursively means telling the server to search this directory, and every directory below it, for the string of text you're looking for. This can be accomplished by adding the -r switch, as seen below:

grep -r "lives"./*

Piping the grep command

So we've looked at how to search for a single word or string of text but how about if we want to search for multiple strings of text or send the contents of the grep command to a different utility for further processing. This is where pipes come in.

A pipe in Linux (found right above the backslash on your keyboard) is a way of telling the operating system to take the output of the command on the left and forward them into being the input of the command on the right That being said, let's go through an example searching out two different words in a file.

grep "lives" declaration | grep frogs

Now this will, of course, return nothing because there is no line in the Declaration of independence with the words "lives" and "frogs" in them. Let's say though that you'd like to see how many times the word "lives" appears in the declaration of independence. Here's how you'd do it:

grep "lives" declaration | wc -l

That's all for today!


No comments:

Post a Comment