{"id":964,"date":"2019-01-25T08:43:44","date_gmt":"2019-01-25T08:43:44","guid":{"rendered":"http:\/\/orissawebhosting.in\/blog\/?p=964"},"modified":"2019-06-23T14:21:32","modified_gmt":"2019-06-23T14:21:32","slug":"use-grep-command-for-searching-in-a-file","status":"publish","type":"post","link":"https:\/\/orissawebhosting.in\/blog\/use-grep-command-for-searching-in-a-file\/","title":{"rendered":"Operate grep command for searching in a file"},"content":{"rendered":"\n<p> In the first example, I will search for the user &#8220;root the Linux passwd file. To search the \/etc\/passwd file for the user &#8220;root&#8221;, you need to enter the following command: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep root  \/etc\/passwd<\/code><\/pre>\n\n\n\n<p>Given below is the sample Output:  root:x:0:0:root:\/root:\/bin\/bash<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"-using-grep-to-search-only-for-words\">Using grep to search only for words<\/h2>\n\n\n\n<p> When you are searching for abc, grep will match all sorts of things, viz., kbcabc, abc123, aarfbc35 and lots more combinations without obeying word boundaries. You can compel the grep command to select only those lines that contain matches to form whole words (those that match only abc word), as shown below: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -w \"abc\" file<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"-using-grep-to-search-two-different-words\">Using grep to search two different words<\/h2>\n\n\n\n<p> To search for two different words, you must use the egrep command as shown below: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>egrep -w 'word1|word2' \/path\/to\/file<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"-count-lines-for-matched-words\"> Count lines for matched words <\/h2>\n\n\n\n<p> The grep command has the ability to report the number of times a particular pattern has been matched for each file using the -c (count) option (as shown below): <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -c 'word' \/path\/to\/file<\/code><\/pre>\n\n\n\n<p>\n\nIn addition, users may use the &#8216;-n&#8217; option preceding each output line with the number of the line in the text file from which it was obtained (as shown below):\n\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -n 'root' \/etc\/passwd<\/code><\/pre>\n\n\n\n<p>  Given below are the Sample outputs:  1:root:x:0:0:root:\/root:\/bin\/bash<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the first example, I will search for the user &#8220;root the Linux passwd file. To search the \/etc\/passwd file for the user &#8220;root&#8221;, you need to enter the following command: Given below is the sample Output: root:x:0:0:root:\/root:\/bin\/bash Using grep to search only for words When you are searching for abc, grep will match all sorts of things, viz., kbcabc, abc123, aarfbc35 and lots more combinations without obeying word boundaries. You can compel the grep command to select only those lines that contain matches to form whole words (those that&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/964"}],"collection":[{"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/comments?post=964"}],"version-history":[{"count":2,"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/964\/revisions"}],"predecessor-version":[{"id":1004,"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/posts\/964\/revisions\/1004"}],"wp:attachment":[{"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/media?parent=964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/categories?post=964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/orissawebhosting.in\/blog\/wp-json\/wp\/v2\/tags?post=964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}