{"id":244,"date":"2010-04-16T08:52:59","date_gmt":"2010-04-16T08:52:59","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2010\/04\/16\/unix-command-grep\/"},"modified":"2018-09-25T09:41:13","modified_gmt":"2018-09-25T09:41:13","slug":"unix-command-grep","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/unix-command-grep\/","title":{"rendered":"Unix Command: Grep &#8211; Quick Reference &#8211; Pattern &#8211; Examples &#8211; Options"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4369\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2010\/04\/unix-command-grep.png\" alt=\"unix-command-grep\" width=\"600\" height=\"400\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2010\/04\/unix-command-grep.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2010\/04\/unix-command-grep-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\"><b>Grep <\/b>scans its input for a pattern, and can display the selected pattern, the line numbers of the filenames where the pattern occurs, The command uses the following syntax<br \/>\n<i><\/i><\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><u><span style=\"font-family: verdana,geneva;\"><i>grep options pattern filesname(s)<\/i><\/span><\/u><span style=\"font-family: verdana,geneva;\"><br \/>\n<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">grep searches for pattern in one or more filenames.<br \/>\n<\/span><\/span><\/p>\n<p><b><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">Example for <u>Grep<\/u> command: <\/span><\/span><\/b><\/p>\n<ol>\n<li><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">grep \u201csales\u201d emp.lst<\/span><\/span><\/li>\n<li><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">grep \u201cdirector\u201d emp1.lst emp2.lst<\/span><\/span><\/li>\n<li><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">grep \u2018jai sharma\u2019 emp.lst<\/span><\/span><\/li>\n<li><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">grep \u201cjai Sharma $var\u201d emp.lst<\/span><\/span><\/li>\n<\/ol>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\"><b>&#8212;&#8212;&#8212;&#8212;&#8212;-Grep options&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/b><\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\"><b>Ignoring Case (i)<\/b> When you look for a name, but you are not sure of the case, grep offers the \u2013i (ignore) option which ignores case for patteen matching.<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">&gt; Grep \u2013i \u2018agarwal\u2019 emp.lst<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\"><b>Deleting Lines\u00a0 or Inverse(-v):<\/b> -v (inverse) option selects all except lines containing the pattern. Thus, you can create a file other list containing all but director.<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">&gt; Grep \u2013v \u201cdirector\u201d emp.lst &gt; other list<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\"><b>Displaying line Numbers (-n): <\/b>The \u2013n(number) options displays the line numbers containing the pattern, along with the lines:<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">&gt; grep \u2013n \u2018marketing\u2019 emp.lst<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\"><b>Counting Line Containing patterns (-c):<\/b> The \u2013c (count) option counts the number of lines containing the pattern ( which is not the same as number of occurrences).<\/span><\/span><\/p>\n<ol>\n<li><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">grep \u2013c director emp.lst<\/span><\/span><\/li>\n<li><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">grep \u2013c director emp*.lst<\/span><\/span><\/li>\n<\/ol>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\"><b>Displaying Filenames (-l):<\/b> The \u2013l (list) option displays only the files names of files containing the pattern.<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">&gt; grep \u2013l \u2018manager\u2019 *.lst<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\"><b>Matching Multiple Patterns(-e):<\/b> With the \u2013e option, you can match the three agarwals by using grep like this:<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">&gt; grep \u2013e \u201cAgarwal\u201d \u2013e \u201caggarwal\u201d \u2013e \u201cagarwal\u201d emp.lst<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\"><b>Taking patterns from a file (-f):<\/b> we can place all patterns in a separate file, one pattern per lin. Grep takes inputs from there with the \u2013f option:<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">&gt; grep \u2013f pattern.lst emp.lst<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: verdana,geneva;\">\u00a0<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grep scans its input for a pattern, and can display the selected pattern, the line numbers of the filenames where the pattern occurs, The command uses the&#8230; <\/p>\n","protected":false},"author":1,"featured_media":4369,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4957],"tags":[4126,333,475,4124,4128,4129,4131,4130,4121,4123,169,4125,4127,4122],"class_list":["post-244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-basic-grep-commands","tag-command","tag-grep","tag-grep-command-line-options","tag-grep-unix-command-guide","tag-grep-unix-command-introduction","tag-grep-unix-command-overview","tag-grep-unix-command-quick-reference","tag-grep-unix-commands-with-examples","tag-how-to-use-grep","tag-unix","tag-unix-command-grep-examples","tag-unix-grep-command-match-pattern","tag-unix-grep-command-options"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":4372,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/244\/revisions\/4372"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/4369"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}