Grep command in linux – Get a grip on it

 grep command in linuxLinux was created by software enthusiasts who wanted to make it available to all for free. The source code was also kept open so that anyone can contribute to it. A Linux loving community was formed which worked on it relentlessly to make it a strong and robust operating system. These people have added commands that made Linux very powerful and popular.

Grep is one such command in Linux which as a user you will find very helpful. Let us see how grep can be used to make your work easier. We assume you have a basic knowledge of Linux. If not, we recommend you first go through this simple Linux crash course.

The history of grep

This command was originally created for the UNIX operating system but later on was applicable to all Unix-based OS and finds particular use in Linux. The name grep comes from the ed command, g/re/p, which stands for “globally search a regular expression and print”. This is pretty much what grep does: it searches for a string or expression and prints all matching lines. We do this type of searches in Windows also, but that just shows you the files. Grep gives you many more options worth exploring. Let’s have a look through them.

How to use grep

The grep command searches files which are specified as arguments. If you do not specify any argument, it searches the standard input of the program. Grep has been programmed to return the results on the standard output, but it also provides options to specify specific mode of operation.

Syntax of grep

Grep takes the following form,

>grep [options] pattern [file]

Let’s understand this better with an example.

        >grep berry fruits.txt

In the above command, we search for all lines in the file, fruits.txt that contain the word berry. When using grep, you need to keep in mind the default behavior of the command. The word boundaries are not considered and hence lines that contain strawberry or blueberry will also be included. You must keep in mind that the function is case sensitive by default and so lines that contain “Berry” will not be included in the result.

It is possible to search in multiple files. This is achieved either by typing the file names one after another with gaps in between or with the use of wild characters also. For example, to search for apple in files, fruit.txt, fruitlist.txt and recipe.txt, the command would be,

        >grep berry fruits.txt fruitlist.txt recipe.txt

The wild characters can also be used with grep. Suppose you want to find the word, “berry” in all the files. In that case, the syntax would be,

>grep berry *.txt

Override the case sensitivity with the option i

As mentioned previously that the grep command is case sensitive. You can override this feature by using the –i option. Let us see how this option is used.

>grep –i Tom Namelist.txt

With this option the grep will search for Tom, tom, TOM and all other combinations.

Match exact words with grep

You can also force this command to match whole words, i.e., for our first example you can make it search for only berry without returning lines that contain blueberry or strawberry. –w option allows you to achieve this.

        >grep –w berry fruitlist.txt

Match exact lines with grep

Similar to matching words, grep can match exact lines also. –x is the option you have to use in such cases.

        >grep –x “berry is good” fruitlist.txt

Use grep recursively

Grep is very flexible and can be to search through sub directories also with the –r options

        >grep –r Tom /etc/namelist

Count the number of occurrences

Other than finding lines that match the string, you may want to find out the number of times the string occurs. The –c option helps you in this.

        >grep –c berry fruitlist.txt

Find the number of the lines that matched your search

You can also find out the number of the lines that matched the string with the help of the –n option.

>grep –n berry fruitlist.txt

Invert match

Grep is very flexible, as you no doubt can see. Let’s look at one more option that is used quite frequently. You can return lines from files that do not match the search string with the help of grep. This is called reverse match and it can be done by using the  –v option.

        >grep –v berry fruitlist.txt

This command line will show you all the lines that do not contain the string ‘berry”.

List of matching files

You may be interested to know the files that contain a particular string rather than the individual lines. In such a situation, you can use –l option.

        >grep –l main *.txt

This command will show you all the files in the directory that contain the text main.

Colorful display

It is also possible to display the result in colors. The colors option is available for this which

>grep — color fruits /etc/fruits/fruitlist

Apart from these, you can perform a variety of operations with grep. It is really an interesting command to explore. We haven’t quite gotten into redirecting the output in different ways. With the massive amount of detail Linux lets get into, it is not possible to include all the options in this article. Just use the ‘man’ command to get all the options, along with info on how to use them.

>man grep

We hope that this tutorial gives you a fair idea of the grep function and all its options. Unlike Windows, the commands in Linux are finer and can take a while to master them. With a little bit extra effort, you’ll also be able to set up your own Linux server.