{"id":783,"date":"2010-02-02T10:04:00","date_gmt":"2010-02-02T15:04:00","guid":{"rendered":"http:\/\/www.gamescheat.ca\/?p=783"},"modified":"2019-09-04T19:57:39","modified_gmt":"2019-09-05T00:57:39","slug":"using-grepegrep-to-find-out-which-file-contains-a-word-or-string-in-unix-or-linux","status":"publish","type":"post","link":"http:\/\/www.gamescheat.ca\/?p=783","title":{"rendered":"Using grep\/egrep to find out which file contains a word or string in Unix or Linux"},"content":{"rendered":"<p>If you need to find out which file in a directory contains a specific word then the grep command is your friend.<\/p>\n<p>Here is an example, if you wanted to find out which file in the directory \/home contains the word groundhog.<\/p>\n<blockquote><div style=\"float: right;\"><div style=\"margin: 15px 15px 15px 15px\";><script type=\"text\/javascript\"><!--\ngoogle_ad_client = \"pub-3319935785736004\";\ngoogle_alternate_color = \"FFFFFF\";\ngoogle_ad_width = 300;\ngoogle_ad_height = 250;\ngoogle_ad_format = \"300x250_as\";\ngoogle_ad_type = \"text_image\";\ngoogle_ad_channel =\"\";\ngoogle_color_border = \"cccccc\";\ngoogle_color_link = \"cc0000\";\ngoogle_color_bg = \"ffffff\";\ngoogle_color_text = \"000000\";\ngoogle_color_url = \"008000\";\n\/\/--><\/script>\n<script type=\"text\/javascript\"\n  src=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\n<\/script><\/div><\/div><p>grep \u2013i \u201cgroundhog\u201d \/home\/*<br \/>\n\/home\/shadow:Today is groundhog day<\/p><\/blockquote>\n<p>The \u2013i option means grep will ignore upper\/lower case distinction during comparisons.<br \/>\nThe word to search for is enclosed in double quotes, \u201cgroundhog\u201d.<br \/>\n\/home is the directory to search in and the * signifies all files.<br \/>\nThe file \/home\/shadow contains the word groundhog, the content of the line containing the word groundhog is displayed on screen.<\/p>\n<p>But what if you wanted to search for a string of words. Grep only searches for a single word.  Egrep will search all files for a string of words.  Here is an excerpt from the man pages of Solaris 10.<\/p>\n<p>The egrep (expression grep) utility  searches  files  for  a<br \/>\npattern of characters and prints all lines that contain that<br \/>\npattern. egrep uses full  regular  expressions  (expressions<br \/>\nthat   has   string   values  that  use  the  full  set  of<br \/>\nalphanumeric and special characters) to match the  patterns.<br \/>\nIt  uses a fast deterministic algorithm that sometimes needs<br \/>\nexponential space.                                           <\/p>\n<p>Below is an example of egrep.<\/p>\n<blockquote><p>egrep \u2013i \u201cApple announces the release of the ipad\u201d \/home\/*<br \/>\n\/home\/obama: Today Apple announces the release of the ipad<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>If you need to find out which file in a directory contains a specific word then the grep command is your friend. Here is an example, if you wanted to find out which file in the directory \/home contains the&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/www.gamescheat.ca\/?p=783\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[21,55,64,35],"_links":{"self":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/783"}],"collection":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=783"}],"version-history":[{"count":3,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":786,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions\/786"}],"wp:attachment":[{"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gamescheat.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}