Utilizing the Grep Command

Posted in WordPress on Jun 24, 2014

The grep command is a uncomplicated Linux command line utility employed for locating strings of textual content in documents. Here we’re going to wander by means of a temporary tutorial on employing the grep command.

Grepping a solitary file

We’re going to assume that we have a one file labeled “declaration” that has section of the Declaration of Independence. Let’s say we’re searching with anything in the file “Declaration” with the word “life” in it. The syntax for the command would be as follows.

grep “lives” declaration

Primarily what this command does is states “glance for the term ‘lives’ in the file ‘declaration'”. Apparently adequate, the grep command will really display the complete line the term was discovered in, so managing this command presents us the next:

“He has plundered our seas, ravaged our Coasts, burnt our cities, and ruined the life of our men and women.”

Grepping a directory

Alright, so let us say that you know some of the text in a file but you happen to be not positive specifically where that file is. Acquiring that file is one particular of the most simple programs of the grep command. To grep for a string of textual content in a listing, use the pursuing:

grep “life”./*

The * functions as a wildcard image in most Linux and Unix operating devices and in essence just tells the grep command to research “All”. In addition to just acquiring the textual content, the grep command, in this instance, will also exhibit you the file it uncovered your string of text in. The output of the command earlier mentioned is as follows:

“./declaration:He has plundered our seas, ravaged our Coasts, burnt our towns, and wrecked the lives of our individuals.”

Grepping Recursively

In certain instances we may well not have the luxurious of figuring out which listing a file is identified in. In this situation, the ability to grep recursively will become useful. Grepping recursively usually means telling the server to lookup this listing, and every single directory underneath it, for the string of text you happen to be searching for. This can be accomplished by incorporating the -r change, as viewed under:

grep -r “lives”./*

Piping the grep command

So we’ve appeared at how to look for for a solitary word or string of text but how about if we want to research for many strings of textual content or deliver the contents of the grep command to a diverse utility for further processing. This is the place pipes appear in.

A pipe in Linux (observed suitable earlier mentioned the backslash on your keyboard) is a way of telling the working program to take the output of the command on the left and forward them into getting the enter of the command on the proper That getting explained, let us go via an example searching out two diverse text in a file.

grep “lives” declaration course, return nothing mainly because there is no line in the Declaration of independence with the text “life” and “frogs” in them. Let’s say even though that you would like to see how a lot of occasions the term “lives” seems in the declaration of independence. Here’s how you would do it:

grep “life” declaration Which is all for right now!

By Robert M Cowher

Leave a Reply

Your email address will not be published. Required fields are marked *