{"id":12405,"date":"2019-03-26T23:02:54","date_gmt":"2019-03-26T23:02:54","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12405"},"modified":"2019-03-26T23:02:54","modified_gmt":"2019-03-26T23:02:54","slug":"whats-difference-between-grep-egrep-and-fgrep-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/26\/whats-difference-between-grep-egrep-and-fgrep-in-linux\/","title":{"rendered":"What\u2019s Difference Between Grep, Egrep and Fgrep in Linux?"},"content":{"rendered":"<p>One of the renowned search tool on Unix-like systems which can be used to search for anything whether it be a file, or a line or multiple lines in file is grep utility. It is very vast in functionality which can be attributed to the large number of options it supports like: searching using string pattern, or reg-ex pattern or perl based reg-ex etc.<\/p>\n<div id=\"attachment_18775\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Difference-Between-grep-egrep-fgrep.png\" rel=\"attachment wp-att-18775\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18775\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Difference-Between-grep-egrep-fgrep.png\" alt=\"Difference Between grep, egrep and fgrep\" width=\"720\" height=\"345\" aria-describedby=\"caption-attachment-18775\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18775\" class=\"wp-caption-text\">Difference Between grep, egrep and fgrep in Linux<\/p>\n<\/div>\n<p>Due its varying functionalities, it has many variants including\u00a0<strong>grep<\/strong>,\u00a0<strong>egrep<\/strong>\u00a0(<strong>Extended GREP<\/strong>),\u00a0<strong>fgrep<\/strong>\u00a0(<strong>Fixed GREP<\/strong>),\u00a0<strong>pgrep<\/strong>\u00a0(<strong>Process GREP<\/strong>),\u00a0<strong>rgrep<\/strong>\u00a0(<strong>Recursive GREP<\/strong>) etc. But these variants have minor differences to original\u00a0<strong>grep<\/strong>which has made them popular and to be used by various Linux programmers for specific tasks.<\/p>\n<p>Main thing that remains to be investigated is what are the differences between the three main variants i.e.\u00a0<strong>\u2018grep\u2019<\/strong>,\u00a0<strong>\u2018egrep\u2019<\/strong>\u00a0and\u00a0<strong>\u2018fgrep\u2019<\/strong>\u00a0of grep that makes Linux users choose one or the other version as per requirement.<\/p>\n<h3>Some Special Meta-Characters of grep<\/h3>\n<ol>\n<li><code>+<\/code>\u00a0\u2013 Equivalent to one or more occurrences of previous character.<\/li>\n<li><code>?<\/code>\u00a0\u2013 This denotes almost 1 repetition of previous character. Like:\u00a0<code>a?<\/code>\u00a0Would match\u00a0<strong>\u2018a\u2019<\/strong>\u00a0or\u00a0<strong>\u2018aa\u2019<\/strong>.<\/li>\n<li><code>(<\/code>\u00a0\u2013 Start of alternation expression.<\/li>\n<li><code>)<\/code>\u00a0\u2013 End of alternation expression.<\/li>\n<li><code>|<\/code>\u00a0\u2013 Matching either of the expression separated by\u00a0<code>'|'<\/code>. Like:\u00a0<code>\u201c(a|b)cde\u201d<\/code>\u00a0would match either\u00a0<strong>\u2018abcde\u2019<\/strong>\u00a0or\u00a0<strong>\u2018bbcde\u2019<\/strong>.<\/li>\n<li><code>{<\/code>\u00a0\u2013 This meta-character indicates start of range specifier. Like:\u00a0<code>\u201ca{2}\u201d<\/code>\u00a0matches\u00a0<strong>\u201caa\u201d<\/strong>\u00a0in file i.e. a 2 times.<\/li>\n<li><code>}<\/code>\u00a0\u2013 This meta-character indicates end of range specifier.<\/li>\n<\/ol>\n<h3>Differences Between grep, egrep and fgrep<\/h3>\n<p>Some main differences between\u00a0<strong>grep<\/strong>,\u00a0<strong>egrep<\/strong>\u00a0and\u00a0<strong>fgrep<\/strong>\u00a0can be highlighted as follows. For this set of examples we are assuming the file on which operation is being performed to be:<\/p>\n<div id=\"attachment_18767\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/grep-command.png\" rel=\"attachment wp-att-18767\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18767\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/grep-command.png\" sizes=\"auto, (max-width: 832px) 100vw, 832px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/grep-command.png 832w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/grep-command-768x306.png 768w\" alt=\"Linux grep Command\" width=\"832\" height=\"332\" aria-describedby=\"caption-attachment-18767\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18767\" class=\"wp-caption-text\">Linux grep Command<\/p>\n<\/div>\n<h4>Grep Command<\/h4>\n<p><strong>grep<\/strong>\u00a0or\u00a0<strong>Global Regular Expression Print<\/strong>\u00a0is the main search program on Unix-like systems which can search for any type of string on any file or list of files or even output of any command.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/12-practical-examples-of-linux-grep-command\/\" target=\"_blank\" rel=\"noopener\">12 Practical Examples of Linux grep Command<\/a><\/p>\n<p>It uses\u00a0<strong>Basic Regular Expressions<\/strong>\u00a0apart from normal strings as a search pattern. In Basic Regular Expressions (BRE), meta-characters like:\u00a0<code>'{','}'<\/code>,<code>'(',')'<\/code>,<code>'|'<\/code>,<code>'+'<\/code>,<code>'?'<\/code>\u00a0loose their meaning and are treated as normal characters of string and need to be escaped if they are to be treated as special characters.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-grep-commands-character-classes-bracket-expressions\/\" target=\"_blank\" rel=\"noopener\">11 Advance \u2018Grep\u2019 Commands on Character Classes and Bracket Expressions<\/a><\/p>\n<p>Also, grep uses Boyer-Moore algorithm for fast searching any string or regular expression.<\/p>\n<pre>$ grep -C 0 '(f|g)ile' check_file\r\n$ grep -C 0 '\\(f\\|g\\)ile' check_file\r\n<\/pre>\n<div id=\"attachment_18768\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Linux-grep-Command-Example.png\" rel=\"attachment wp-att-18768\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18768\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Linux-grep-Command-Example.png\" alt=\"Linux grep Command Example\" width=\"580\" height=\"138\" aria-describedby=\"caption-attachment-18768\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18768\" class=\"wp-caption-text\">Linux grep Command Example<\/p>\n<\/div>\n<p>Like here, when the command is run without escaping\u00a0<code>'(' ')'<\/code>\u00a0and\u00a0<code>'|'<\/code>\u00a0then it searched for the complete string i.e.\u00a0<code>\u201c(f|g)ile\u201d<\/code>\u00a0in the file. But when the special characters were escaped, then instead of treating them as part of string, grep treated them as meta-characters and searched for words\u00a0<code>\u201cfile\u201d<\/code>\u00a0or\u00a0<code>\u201cgile\u201d<\/code>\u00a0in the file.<\/p>\n<h4>Egrep Command<\/h4>\n<p><strong>Egrep<\/strong>\u00a0or\u00a0<strong>grep<\/strong>\u00a0<strong>-E<\/strong>\u00a0is another version of grep or the Extended grep. This version of grep is efficient and fast when it comes to searching for a regular expression pattern as it treats meta-characters as is and doesn\u2019t substitute them as strings like in grep, and hence you are freed from the burden of escaping them as in grep. It uses ERE or the Extended Regular Expression set.<\/p>\n<p>In case of egrep, even if you do not escape the meta-characters, it would treat them as special characters and substitute them for their special meaning instead of treating them as part of string.<\/p>\n<pre>$ egrep -C 0 '(f|g)ile' check_file\r\n$ egrep -C 0 '\\(f\\|g\\)ile' check_file\r\n<\/pre>\n<div id=\"attachment_18769\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Linux-egrep-Command-Examples.png\" rel=\"attachment wp-att-18769\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18769\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Linux-egrep-Command-Examples.png\" alt=\"Linux egrep Command Examples\" width=\"617\" height=\"140\" aria-describedby=\"caption-attachment-18769\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18769\" class=\"wp-caption-text\">Linux egrep Command Examples<\/p>\n<\/div>\n<p>Like here,\u00a0<strong>egrep<\/strong>\u00a0searched for\u00a0<code>\u201cfile\u201d<\/code>\u00a0string when the meta-characters were not escaped as it would mean by the meaning of these characters. But, when these characters were escaped, then egrep treated them as part of string and searched for complete string\u00a0<code>\u201c(f|g)ile\u201d<\/code>\u00a0in the file.<\/p>\n<h4>fgrep Command<\/h4>\n<p><strong>Fgrep<\/strong>\u00a0or the\u00a0<strong>Fixed grep<\/strong>\u00a0or\u00a0<strong>grep -F<\/strong>\u00a0is yet another version of grep which is fast in searching when it comes to search for the entire string instead of regular expression as it doesn\u2019t recognize the regular expressions, neither any meta-characters. For searching any direct string, this is the version of grep which should be selected.<\/p>\n<p>Fgrep searches for complete string and doesn\u2019t even recognize special characters as part of regular expression even if escaped or not escaped.<\/p>\n<pre>$ fgrep -C 0 '(f|g)ile' check_file\r\n$ fgrep -C 0 '\\(f\\|g\\)ile' check_file\r\n<\/pre>\n<div id=\"attachment_18770\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Linux-fgrep-Command-Examples.png\" rel=\"attachment wp-att-18770\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18770\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/02\/Linux-fgrep-Command-Examples.png\" alt=\"Linux fgrep Command Examples\" width=\"657\" height=\"113\" aria-describedby=\"caption-attachment-18770\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18770\" class=\"wp-caption-text\">Linux fgrep Command Examples<\/p>\n<\/div>\n<p>Like, when meta-characters were not escaped, fgrep searched for the complete string\u00a0<code>\u201c(f|g)ile\u201d<\/code>\u00a0in the file, and when the meta-characters were escaped, then the fgrep command searched for\u00a0<code>\u201c\\(f\\|g\\)ile\u201d<\/code>\u00a0all characters as is in the file.<\/p>\n<p>We\u2019ve already covered some practical examples of grep command you can read them here, if you want to get more out of grep command in Linux.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/manage-file-types-and-set-system-time-in-linux\/\" target=\"_blank\" rel=\"noopener\">Learn 12 Practical Examples of Linux grep Command<\/a><\/p>\n<h3>Conclusion<\/h3>\n<p>Above highlighted are the differences between\u00a0<strong>\u2018grep\u2019<\/strong>,\u00a0<strong>\u2018egrep\u2019<\/strong>\u00a0and\u00a0<strong>\u2018fgrep\u2019<\/strong>. Apart from difference in the set of regular expressions used, and speed of execution, rest command line parameters remain same for all the three versions of grep and even instead of \u201cegrep\u201d or \u201cfgrep\u201d, \u201cgrep -E\u201d or \u201cgrep -F\u201d are recommended to be used.<\/p>\n<p>If you find any other differences between these three versions of grep, do mention them in your comments.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/difference-between-grep-egrep-and-fgrep-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the renowned search tool on Unix-like systems which can be used to search for anything whether it be a file, or a line or multiple lines in file is grep utility. It is very vast in functionality which can be attributed to the large number of options it supports like: searching using string &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/26\/whats-difference-between-grep-egrep-and-fgrep-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;What\u2019s Difference Between Grep, Egrep and Fgrep in Linux?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12405","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12405"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12405\/revisions"}],"predecessor-version":[{"id":12406,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12405\/revisions\/12406"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}