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:
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.
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
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
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:
$ ls -l | grep -i “searchword”
$ 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 a String in a File
You can fetch a sentence from a file that contains a specific string of text through the grep command.
grep “string” filename
$ grep “sample file” sampleFile.txt
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.
$ grep “string” filenameKeyword*
$ grep “string” *.extension
$ grep "sample file” sample*
This command will fetch all the sentences containing the string “sample file” from all the files with the filename containing the keyword “sample”.
$ grep "sample file” *.txt
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
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.
$ grep -i “string” filename
$ grep -i "Sample file" *.txt
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.
$ grep “startingKeyword.*endingKeyword” filename
$ grep "starting.*.ending" sampleFile.txt
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
$ grep -A 3 -i "samplestring" sampleFile.txt
This is how my sample text file looks like:
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.