{"id":3498,"date":"2018-11-16T15:12:32","date_gmt":"2018-11-16T15:12:32","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=3498"},"modified":"2018-11-17T15:32:20","modified_gmt":"2018-11-17T15:32:20","slug":"use-of-the-grep-command-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/16\/use-of-the-grep-command-in-linux\/","title":{"rendered":"Use of the grep Command in Linux"},"content":{"rendered":"<h2><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Using grep Command in Linux\" width=\"750\" height=\"375\" \/>What is grep?<\/h2>\n<p>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.<\/p>\n<p>So they say GREP stands for Global Regular Expression Print but where does this command \u2018grep\u2019 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:<\/p>\n<p><em>g\/re\/p<\/em><\/p>\n<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.<\/p>\n<p>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.<\/p>\n<p>We have run the commands and procedures mentioned in this article on an Ubuntu 18.04 LTS system.<\/p>\n<h2>Install grep<\/h2>\n<p>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:<\/p>\n<p>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:<\/p>\n<p>$ sudo apt-get install grep<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Install grep command\" width=\"367\" height=\"125\" \/><\/p>\n<p>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.<\/p>\n<p>You can verify the installation by checking the grep version through the following command:<\/p>\n<p>$ grep &#8211;version<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Check grep command version\" width=\"531\" height=\"181\" \/><\/p>\n<h2>Use of the grep Command with Examples<\/h2>\n<p>The grep command can be best explained by presenting some scenarios where it can be made use of. Here are a few examples:<\/p>\n<h3>Search for Files<\/h3>\n<p>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:<\/p>\n<p>Syntax:<\/p>\n<p><em>$ ls -l | grep -i \u201c<\/em>searchword<em>\u201d<\/em><\/p>\n<p>Examples:<\/p>\n<p>$ ls -l | grep -i sample<\/p>\n<p>This command will list all the files in the current directory with the name of the file containing the word \u201cprivate\u201d.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Search for files with grep\" width=\"613\" height=\"75\" \/><\/p>\n<h3>Search for a String in a File<\/h3>\n<p>You can fetch a sentence from a file that contains a specific string of text through the grep command.<\/p>\n<p>Syntax:<\/p>\n<p><em>grep \u201cstring\u201d filename<\/em><\/p>\n<p>Example:<\/p>\n<p>$ grep \u201csample file\u201d sampleFile.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Search for text in a file with grep\" width=\"635\" height=\"43\" \/><\/p>\n<p>My sample file sampleFile.txt contains the sentence having the string \u201csample file\u201d that you can see in the above output. The keyword and string appear in a colored form in the search results.<\/p>\n<h3>Search for a String in More Than One File<\/h3>\n<p>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.<\/p>\n<p>Syntax 1:<\/p>\n<p><em>$ grep \u201cstring\u201d filenameKeyword*<\/em><\/p>\n<p>Syntax 2:<\/p>\n<p><em>$ grep \u201cstring\u201d *.extension<\/em><\/p>\n<p>Example1:<\/p>\n<p>$ grep &#8220;sample file\u201d sample*<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Search for a String in More Than One File\" width=\"730\" height=\"144\" \/><\/p>\n<p>This command will fetch all the sentences containing the string \u201csample file\u201d from all the files with the filename containing the keyword \u201csample\u201d.<\/p>\n<p>Example 2:<\/p>\n<p>$ grep &#8220;sample file\u201d *.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Search for a String in More Than One File - Example 2\" width=\"732\" height=\"95\" \/><\/p>\n<p>This command will fetch all the sentences containing the string \u201csample file\u201d from all the files with .txt extension.<\/p>\n<h3>Search for a String in a File Without Taking in Account the Case of the String<\/h3>\n<p>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 \u201cSample\u201d<\/p>\n<p>$ grep &#8220;Sample file&#8221; *.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Search with case sensitive string\" width=\"354\" height=\"36\" \/><\/p>\n<p>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.<\/p>\n<p>Syntax:<\/p>\n<p><em>$ grep -i \u201cstring\u201d filename<\/em><\/p>\n<p>Example:<\/p>\n<p>$ grep -i &#8220;Sample file&#8221; *.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Case insensitive search with grep command\" width=\"728\" height=\"96\" \/><\/p>\n<p>This command will fetch all the sentences containing the string \u201csample file\u201d from all the files with .txt extension. This will not take into account whether the search string was in upper or lower case.<\/p>\n<h3>Search on the basis of a regular expression<\/h3>\n<p>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.<\/p>\n<p>Syntax:<\/p>\n<p><em>$ grep \u201cstartingKeyword.*endingKeyword\u201d filename<\/em><\/p>\n<p>Example:<\/p>\n<p>$ grep &#8220;starting.*.ending&#8221; sampleFile.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"Use regular expressions in grep\" width=\"593\" height=\"69\" \/><\/p>\n<p>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.<\/p>\n<h3>Display a Specified Number of Lines After\/Before the Search String<\/h3>\n<p>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.<\/p>\n<p>The syntax for N number of lines after the key string:<\/p>\n<p><em>$ grep -A &lt;N&gt; \u201cstring\u201d filename<\/em><\/p>\n<p>Example:<\/p>\n<p>$ grep -A 3 -i &#8220;samplestring&#8221; sampleFile.txt<\/p>\n<p>This is how my sample text file looks like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" alt=\"sample text file\" width=\"561\" height=\"259\" \/><\/p>\n<p>And this is how the output of the command looks like:<\/p>\n<p><img decoding=\"async\" src=\"\/\/vitux.com\/wp-content\/plugins\/a3-lazy-load\/assets\/images\/lazy_placeholder.gif\" \/><\/p>\n<p>It displays 3 lines, including the one containing the searched string, from the file I specified in the grep command.<\/p>\n<p>The syntax for N number of lines before the key string:<\/p>\n<p><em>$ grep -B &lt;N&gt; \u201cstring\u201d filename<\/em><\/p>\n<p>You can also search for N number of lines \u2018around\u2019 a text string. That means N number of lines before and N after the text string.<\/p>\n<p>The syntax for N number of lines around the key string:<\/p>\n<p><em>$ grep -C &lt;N&gt; \u201cstring\u201d filename<\/em><\/p>\n<p>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.<\/p>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=262894\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/16\/use-of-the-grep-command-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Use of the grep Command in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3498","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=3498"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3498\/revisions"}],"predecessor-version":[{"id":3751,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/3498\/revisions\/3751"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=3498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=3498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=3498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}