Using grep Command in Linux

What is grep?

The grep utility that we will be getting a hold of today is a Unix tool that belongs to the same family as the egrep and fgrep utilities. These are all Unix tools designed for performing the repetitive searching task on your files and text. You can search for files and their contents for useful information fetching by specifying particular search criteria through the grep command.

So they say GREP stands for Global Regular Expression Print but where does this command ‘grep’ originate from? grep basically derives from a specific command for the very simple and venerable Unix text editor named ed. This is how the ed command goes:

g/re/p

The purpose of the command is pretty similar to what we mean by searching through grep. This command fetches all the lines in a file matching a certain text pattern.

Let us explore the grep command some more. In this article, we will explain the installation of the grep utility and present some examples through which you can learn exactly how and in which scenario you can use it.

We have run the commands and procedures mentioned in this article on an Ubuntu 18.04 LTS system.

Install grep

Although the grep utility comes by default with most Linux systems, if you do not have it installed on your system, here is the procedure:

Open your Ubuntu Terminal either through the Dash or the Ctrl+Alt+T shortcut. Then enter the following command as root in order to install grep through apt-get:

$ sudo apt-get install grep

Install grep command

Enter y when you are prompted with a y/n option during the installation procedure. After that, the grep utility will be installed on your system.

You can verify the installation by checking the grep version through the following command:

$ grep --version

Check grep command version

Use of the grep Command with Examples

The grep command can be best explained by presenting some scenarios where it can be made use of. Here are a few examples:

Search for Files

If you want to search for a filename that contains a specific keyword, you can filter your file list through the grep command as follows:

Syntax:

$ ls -l | grep -i “searchword

Examples:

$ ls -l | grep -i sample

This command will list all the files in the current directory with the name of the file containing the word “private”.

Search for files with grep

Search for a String in a File

You can fetch a sentence from a file that contains a specific string of text through the grep command.

Syntax:

grep “string” filename

Example:

$ grep “sample file” sampleFile.txt

Search for text in a file with grep

My sample file sampleFile.txt contains the sentence having the string “sample file” that you can see in the above output. The keyword and string appear in a colored form in the search results.

Search for a String in More Than One File

In case you want to search for sentences containing your text string from all the files of the same type, grep command is at your service.

Syntax 1:

$ grep “string” filenameKeyword*

Syntax 2:

$ grep “string” *.extension

Example1:

$ grep "sample file” sample*

Search for a String in More Than One File

This command will fetch all the sentences containing the string “sample file” from all the files with the filename containing the keyword “sample”.

Example 2:

$ grep "sample file” *.txt

Search for a String in More Than One File - Example 2

This command will fetch all the sentences containing the string “sample file” from all the files with .txt extension.

Search for a String in a File Without Taking in Account the Case of the String

In the above examples, my text string was luckily in the same case as that found in my sample text files. If I had entered the following command, my search result would be nil because the text in my file does not start with an upper-case word “Sample”

$ grep "Sample file" *.txt

Search with case sensitive string

Let us tell grep to ignore the case of the search string and print the search results based on the string through the -i option.

Syntax:

$ grep -i “string” filename

Example:

$ grep -i "Sample file" *.txt

Case insensitive search with grep command

This command will fetch all the sentences containing the string “sample file” from all the files with .txt extension. This will not take into account whether the search string was in upper or lower case.

Search on the basis of a regular expression

Through the grep command, you can specify a regular expression with a start and end keyword. The output will be the sentence containing the entire expression between your specified starting and ending keyword. This feature is very powerful as you do not need to write an entire expression in the search command.

Syntax:

$ grep “startingKeyword.*endingKeyword” filename

Example:

$ grep "starting.*.ending" sampleFile.txt

Use regular expressions in grep

This command will print the sentence containing the expression(starting from my startingKeyword and ending on my endingKeyword) from the file that I specified in the grep command.

Display a Specified Number of Lines After/Before the Search String

You can use the grep command to print N number of lines before/after a search string from a file. The search result also includes the line of text containing the search string.

The syntax for N number of lines after the key string:

$ grep -A <N> “string” filename

Example:

$ grep -A 3 -i "samplestring" sampleFile.txt

This is how my sample text file looks like:

sample text file

And this is how the output of the command looks like:

It displays 3 lines, including the one containing the searched string, from the file I specified in the grep command.

The syntax for N number of lines before the key string:

$ grep -B <N> “string” filename

You can also search for N number of lines ‘around’ a text string. That means N number of lines before and N after the text string.

The syntax for N number of lines around the key string:

$ grep -C <N> “string” filename

Through the simple examples described in this article, you can have a grip on the grep command. You can then use it to search filtered results that may include files or contents of the file. This saves a lot of time that was wasted on skimming through the entire search results before you mastered the grep command.

Use of the grep Command in Linux