{"id":204,"date":"2021-07-14T07:41:00","date_gmt":"2021-07-14T07:41:00","guid":{"rendered":"https:\/\/tiruppurwebhosting.com\/blog\/?p=204"},"modified":"2021-07-14T07:46:19","modified_gmt":"2021-07-14T07:46:19","slug":"use-the-grep-commands","status":"publish","type":"post","link":"https:\/\/tiruppurwebhosting.com\/blog\/use-the-grep-commands\/","title":{"rendered":"How to use the GREP commands in Linux"},"content":{"rendered":"<p>How do I use grep command in Linux operating systems?<\/p>\n<p>The\u00a0grep command\u00a0which stands for \u201cglobal regular expression print,\u201d processes text line by line and prints any lines which match a specified pattern.<\/p>\n<p>It is a command line utility for searching plain text data sets for lines that match a regular expression.<\/p>\n<p>The grep command searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines.In addition, three variant programs <strong>egrep,<\/strong> <strong>fgrep<\/strong> and <strong>rgrep<\/strong> are available. <strong>egrep<\/strong> is the same as <strong>grep -E<\/strong>. <strong>fgrep<\/strong> is the same as <strong>grep -F<\/strong>. <strong>rgrep<\/strong> is the same as <strong>grep -r<\/strong>. Direct invocation as either <strong>egrep<\/strong> or <strong>fgrep<\/strong> is deprecated, but is provided to allow historical applications that rely on them to run unmodified<\/p>\n<p>Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.<\/p>\n<p>The name, \u201cgrep\u201d, derives from the command used to perform a similar operation, using the <a href=\"https:\/\/www.squarebrothers.com\/linux-web-hosting-india\/\" target=\"_blank\" rel=\"noopener\">Linux<\/a> text<\/p>\n<p>editor ed:<\/p>\n<pre>g\/re\/p<\/pre>\n<h4>grep Command Syntax<\/h4>\n<pre> grep 'word' filename\r\n grep 'string1 string2' filename\r\n cat otherfile | grep 'something'\r\n command | grep 'something'\r\n command option1 | grep 'data'\r\n grep --color 'data' fileName<\/pre>\n<h4>How Do I Use grep To Search File?<\/h4>\n<p>Search \/etc\/passwd for online user:<\/p>\n<pre>$ grep online \/etc\/passwd<\/pre>\n<p>You can force grep to ignore word case i.e match online, Online,ONLINE and all other combination with -i option:<\/p>\n<pre>$ grep -i \"online\" \/etc\/passwd<\/pre>\n<h4>Use grep recursively<\/h4>\n<p>You can search recursively i.e. read all files under each directory for a string \u201c111.100.1.1\u201d<\/p>\n<pre>$ grep -r \"111.100.1.1\" \/etc\/<\/pre>\n<h4>Use grep to search words only<\/h4>\n<p>When you search for online, grep will match useronline, online123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only online word:<\/p>\n<pre>$ grep -w \"online\" \/path\/to\/file<\/pre>\n<h4>Use grep to search 2 different words<\/h4>\n<p>use egrep as follows:<\/p>\n<pre>$ egrep -w 'word1|word2' \/path\/to\/file<\/pre>\n<h4>Count line when words has been matched<\/h4>\n<p>grep can report the number of times that the pattern has been matched for each file using -c (count) option:<\/p>\n<pre>$ grep -c 'word' \/path\/to\/file<\/pre>\n<p>Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained:<\/p>\n<pre>$ grep -n 'word' \/path\/to\/file<\/pre>\n<h4>Grep invert match<\/h4>\n<p>You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:<\/p>\n<pre>$ grep -v bar \/path\/to\/file<\/pre>\n<h4>Linux pipes and grep command<\/h4>\n<p>grep command often used with pipes. For example print name of hard disk devices:<\/p>\n<pre># dmesg | egrep '(s|h)d[a-z]'<\/pre>\n<p>Display cpu model name:<\/p>\n<pre># cat \/proc\/cpuinfo | grep -i 'Model'<\/pre>\n<p>However, above command can be also used as follows without shell pipe:<\/p>\n<pre># grep -i 'Model' \/proc\/cpuinfo<\/pre>\n<h4>How do I list just the names of matching files?<\/h4>\n<p>Use the -l option to list file name whose contents mention main():<\/p>\n<pre>$ grep -l 'main' *.c<\/pre>\n<p>Atlast, you can force grep to display output in colors:<\/p>\n<pre>$ grep --color vivek \/etc\/passwd<\/pre>\n<p>Apart from the above if you want to delete files and folders via command SSH you can <a href=\"https:\/\/tiruppurwebhosting.com\/blog\/2021\/06\/30\/how-to-search-files-and-folders-via-ssh\/\">click here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do I use grep command in Linux operating systems? The\u00a0grep command\u00a0which stands for \u201cglobal regular expression print,\u201d processes text line by line and prints any lines which match a specified pattern. It is a command line utility for searching plain text data sets for lines that match a regular expression. The grep command searches<\/p>\n","protected":false},"author":1,"featured_media":207,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,92,1],"tags":[110,112,111],"class_list":["post-204","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basics","category-general","category-home","tag-command","tag-global-user","tag-online"],"_links":{"self":[{"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":0,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/media\/207"}],"wp:attachment":[{"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}