{"id":2164,"date":"2017-03-18T04:07:59","date_gmt":"2017-03-18T04:07:59","guid":{"rendered":"http:\/\/www.fastwebhost.in\/blog\/?p=2164"},"modified":"2019-01-22T12:12:47","modified_gmt":"2019-01-22T12:12:47","slug":"grep-command-in-unix-linux-with-11-simple-examples","status":"publish","type":"post","link":"https:\/\/www.fastwebhost.in\/blog\/grep-command-in-unix-linux-with-11-simple-examples\/","title":{"rendered":"Grep Command in Unix\/Linux with 11 Simple Examples"},"content":{"rendered":"<h2>Description for grep command in Unix\/Linux<\/h2>\n<p><strong>Grep command in Unix\/Linux<\/strong>&nbsp;is a powerful tool that searches for matching a regular expression against text in a file, multiple files or a stream of input. It searches for the pattern of text that you specify on the command line and prints output for you.<\/p>\n<p>In addition, three variant programs egrep,fgrep and rgrep are available.<\/p>\n<ul>\n<li><span>egrep<\/span> is the same as grep -E<\/li>\n<li>&nbsp;<span>fgrep<\/span> is the same as grep -F<\/li>\n<li><span>rgrep<\/span> is the same as grep -r<\/li>\n<\/ul>\n<p>Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.<\/p>\n<p>Example of grep command in Unix\/Linux: Let\u2019s say if you quickly want to search the string \u201clinux\u201d in .html files on your machine? Let\u2019s start by searching a single file.<\/p>\n<p>Here, our PATTERN string &nbsp;is \u201clinux\u201d and our FILE is index.html<\/p>\n<h2>1. Search for a string \u201clinux\u201d in a file using &nbsp;grep command in unix<\/h2>\n<p>This is the basic usage of grep command. It searches for the given string in the specified file.<\/p>\n<p>grep \u201clinux\u201d index.html<\/p>\n<h2>2. Insensitive case search with grep -i<\/h2>\n<p>The below grep command searches for the words like \u201c<strong>LINUX<\/strong>\u201d, \u201c<strong>Linux<\/strong>\u201d, \u201c<strong>linux<\/strong>\u201d case insensitively.<\/p>\n<p>grep -i \u201clinux\u201d index.html<\/p>\n<h2>3. Searching for a string in multiple files.<\/h2>\n<p>This command will search for &#8220;linux&#8221; string in multiple files at a time. It searches in all files with file1.txt, file2.txt and along with different extensions too like file1.html, file2.php and so on.<\/p>\n<p>grep \u201clinux\u201d file*.*<\/p>\n<h2>4. Specifying the search string as a regular expression pattern.<\/h2>\n<p>It is a very powerful feature and can use as a regular expression with much effectively. In the below example, it searches for all the pattern that starts with \u201cfast\u201d and ends with \u201chost\u201d with anything in-between. i.e To search \u201cfast[anything in-between]host\u201d in index.html file.<\/p>\n<p>grep \u201cfast.*host\u201d index.html<\/p>\n<h2>5. Displaying the line numbers.<\/h2>\n<p>You can use this grep command to display the line number which contains the matched string in a file using the -n option<\/p>\n<p>grep -n \u201cword*\u201d file.txt<\/p>\n<h2>6. Highlighting the search using grep<\/h2>\n<p>If we use the \u2013color option, our successful matches will be highlighted for us.<\/p>\n<p>grep \u2013color \u201clinux\u201d index.html<\/p>\n<h2>7. Print the line excluding the pattern using -v option<\/h2>\n<p>List all the lines of the file \/etc\/passwd that does not contain specific word \u201cstring\u201d.<\/p>\n<p>grep -v linux \/etc\/passwd<\/p>\n<h2>8. Display all the lines that starts with specified pattern using ^ symbol<\/h2>\n<p>Bash shell treats carrot symbol (^) as a special character which treat as the beginning of line. Let\u2019s display the lines which starts with \u201croot\u201d word in the file \/etc\/passwd.<\/p>\n<p>grep ^root \/etc\/passwd<\/p>\n<h2>9. Display all the lines that ends with specified pattern using $ symbol.<\/h2>\n<p>List all the lines of \/etc\/passwd that ends with \u201cbash\u201d word.<\/p>\n<p>grep bash$ \/etc\/passwd<\/p>\n<h2>10. Search the pattern recursively using -r option<\/h2>\n<p>The below command will search linux in the \u201c\/etc\u201d directory recursively.<\/p>\n<p>grep -r linux \/etc\/<\/p>\n<h2>11. Counting the lines when words match<\/h2>\n<p>\u200b\u200bThis grep command can report the number of times the pattern matches for each file by using -c (count) option.<\/p>\n<p>\u200bgrep -c &#8216;test&#8217; \/home\/example\/test.txt<\/p>\n<h2>Conclusion<\/h2>\n<p>By the time you complete reading this article, I am sure you will get basic idea about grep command and how to use it on your Linux server with different search patterns. If you like this, please post your comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description for grep command in Unix\/Linux Grep command in Unix\/Linux&nbsp;is a powerful tool that searches for matching a regular expression against text in a file, multiple files or a stream of input. It searches for the pattern of text that you specify on the command line and prints output for you. In addition, three variant [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":2238,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[22],"tags":[111,110,112],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/2164"}],"collection":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/comments?post=2164"}],"version-history":[{"count":3,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/2164\/revisions"}],"predecessor-version":[{"id":4716,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/2164\/revisions\/4716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media\/2238"}],"wp:attachment":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media?parent=2164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/categories?post=2164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/tags?post=2164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}