{"id":36,"date":"2019-07-05T17:05:28","date_gmt":"2019-07-05T11:35:28","guid":{"rendered":"http:\/\/techieshouts.com\/?p=36"},"modified":"2022-08-09T19:09:06","modified_gmt":"2022-08-09T13:39:06","slug":"search-string-or-substring-in-a-file-using-shell","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/search-string-or-substring-in-a-file-using-shell\/","title":{"rendered":"Search String or substring in a file using shell"},"content":{"rendered":"\n<p>We will see the usage of grep command in different cases. The command can be used in different ways based on the need to search a string or substring in a file or in multiple files in a folder.<\/p>\n\n\n\n<p> The <strong>grep<\/strong> command stands for  &#8220;global regular expression print&#8221;<\/p>\n\n\n\n<p><strong>General format<\/strong><\/p>\n\n\n\n<pre data-mode=\"scss\" data-theme=\"solarized_light\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">grep [-options] [directory\/single file] [pattern\/-e] [string to search]<\/pre>\n\n\n\n<p>To see the full syntax and various options, try &#8220;<strong>grep &#8211;help<\/strong>&#8220;<\/p>\n\n\n\n<p>To search the string in a single file, <\/p>\n\n\n\n<p><strong>grep file1.txt -e &#8216;techieshouts&#8217;<\/strong><\/p>\n\n\n\n<p>To search the full word and not the substring,<\/p>\n\n\n\n<p><strong>grep -w file1.txt -e &#8216;techieshouts&#8217;<\/strong><\/p>\n\n\n\n<p>To search the full word by ignoring case sensitiveness,<\/p>\n\n\n\n<p><strong>grep -wi file1.txt -e &#8216;techieshouts&#8217;<\/strong><\/p>\n\n\n\n<p>To search all the files recursively in the directory for the same word,<\/p>\n\n\n\n<p><strong>grep -wir \/directory -e &#8216;techieshouts&#8217;<\/strong><\/p>\n\n\n\n<p>To search and get the line number of the word in all the files<\/p>\n\n\n\n<p><strong>grep -wirn \/directory -e &#8216;techieshouts&#8217;<\/strong><\/p>\n\n\n\n<p>To search and get only the file names that contain the word<\/p>\n\n\n\n<p><strong>grep -wirl \/directory -e &#8216;techieshouts&#8217;<\/strong><\/p>\n\n\n\n\n\n<p>Also, check &#8220;<a href=\"https:\/\/techieshouts.com\/using-bteq-export-table-to-file-in-teradata\/\">Export Teradata table to file using BTEQ<\/a>&#8220;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We will see the usage of grep command in different cases. The command can be used in different ways based on the need to search a string or substring in a file or in multiple files in a folder. The grep command stands for &#8220;global regular expression print&#8221; General format To see the full syntax\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/search-string-or-substring-in-a-file-using-shell\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,16],"tags":[29,27,28],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/36"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":17,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/36\/revisions\/581"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}