{"id":1902,"date":"2017-03-23T18:44:27","date_gmt":"2017-03-23T13:14:27","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1902"},"modified":"2017-03-28T17:51:26","modified_gmt":"2017-03-28T12:21:26","slug":"difference-grep-egrep-fgrep","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/difference-grep-egrep-fgrep\/","title":{"rendered":"BASH &#8211; What is the difference between `grep`, `egrep`, and `fgrep`?"},"content":{"rendered":"<h4 id=\"grep\"><span style=\"color: #ff6600;\"><strong>Grep:<\/strong><\/span><\/h4>\n<ul>\n<li>Grep (Global Regular Expression Print) is a command-line utility for searching plain-text data sets for lines matching a regular expression. &#8230; Grep was originally developed for the Unix operating system, but is available today for all Unix-like systems.<\/li>\n<\/ul>\n<h4 id=\"egrep\"><strong><span style=\"color: #808000;\">egrep<\/span><\/strong><\/h4>\n<ul>\n<li>egrep is an acronym for &#8220;Extended Global Regular Expressions Print&#8221;. It is a program which scans a specified file line by line, returning lines that contain a pattern matching a given regular expression. The standard egrep command looks like: egrep &lt;flags&gt; &#8216;&lt;regular expression&gt;&#8217; &lt;filename&gt;<\/li>\n<\/ul>\n<h4 id=\"fgrep\"><strong><span style=\"color: #800080;\">fgrep<\/span><\/strong><\/h4>\n<ul>\n<li>The fgrep command differs from the grep and egrep commands because it searches for a string instead of searching for a pattern that matches an expression.<\/li>\n<li>The fgrep command uses a fast and compact algorithm. The $, *, [, &#8230; , (, ), and \\ characters are interpreted literally by the fgrep command.<\/li>\n<\/ul>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"difference-between-grep-egrep-and-fgrep\"><span style=\"color: #ff6600;\"><strong>difference between `grep`, `egrep`, and `fgrep`:<\/strong><\/span><\/h4>\n<h4 id=\"grep-2\"><strong><span style=\"color: #808000;\">grep:<\/span><\/strong><\/h4>\n<ul>\n<li>grep is an acronym that stands for &#8220;Global Regular Expressions Print&#8221;. grep is a program which scans a specified file or files line by line, returning lines that contain a pattern.<\/li>\n<li>A pattern is an expression that specifies a set of strings by interpreting characters as meta-characters.<\/li>\n<li>For example the asterisk meta character (*) is interpreted as meaning &#8220;zero or more of the preceding element&#8221;.<\/li>\n<li>This enables users to type a short series of characters and meta characters into a grep command to have the computer show us what lines in which files match.<\/li>\n<\/ul>\n<h4 id=\"the-standard-grep-command-looks-like\"><strong><span style=\"color: #800080;\">The standard grep command looks like:<\/span><\/strong><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">grep &lt;flags&gt; &#039;&lt;regular expression&gt;&#039; &lt;filename&gt;<\/code><\/pre> <\/div>\n<p><span style=\"color: #ff6600;\"><strong>grep prints the search results to the screen (stdout) and returns the following exit values:<\/strong><\/span><\/p>\n<p>0 A match was found.<br \/>\n1 No match was found.<br \/>\n&gt;1 A syntax error was found or a file was inaccessible<br \/>\n(even if matches were found).<\/p>\n<p>Some common flags are: -c for counting the number of successful matches and not printing the actual matches, -i to make the search case insensitive, -n to print the line number before each match printout, -v to take the complement of the regular expression (i.e. return the lines which don&#8217;t match), and -l to print the file names of files with lines which match the expression.<\/p>\n<h4 id=\"egrep-2\"><strong><span style=\"color: #808000;\">egrep<\/span><\/strong><\/h4>\n<ul>\n<li>egrep is an acronym that stands for &#8220;Extended Global Regular Expressions Print&#8221;.<\/li>\n<li>The &#8216;E&#8217; in egrep means treat the pattern as a regular expression. &#8220;Extended Regular Expressions&#8221; abbreviated &#8216;ERE&#8217; is enabled in egrep. egrep (which is the same as grep -E) treats +, ?, |, (, and ) as meta-characters.<\/li>\n<li>In basic regular expressions (with grep), the meta-characters ?, +, {, |, (, and ) lose their special meaning. If you want grep to treat these characters as meta-characters, escape them \\?, \\+, \\{, \\|, \\(, and \\).<\/li>\n<li>For example, here grep uses basic regular expressions where the plus is treated literally, any line with a plus in it is returned.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">grep &quot;+&quot; myfile.txt<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>egrep on the other hand treats the &#8220;+&#8221; as a meta character and returns every line because plus is interpreted as &#8220;one or more times&#8221;.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">egrep &quot;+&quot; myfile.txt<\/code><\/pre> <\/div>\n<p>Here every line is returned because the + was treated by egrep as a meta character. normal grep would have searched only for lines with a literal +.<\/p>\n<h4 id=\"fgrep-2\"><span style=\"color: #800080;\"><strong>fgrep<\/strong><\/span><\/h4>\n<ul>\n<li>fgrep is an acronym that stands for &#8220;Fixed-string Global Regular Expressions Print&#8221;.<\/li>\n<li>fgrep (which is the same as grep -F) is fixed or fast grep and behaves as grep but does NOT recognize any regular expression meta-characters as being special. The search will complete faster because it only processes a simple string rather than a complex pattern.<\/li>\n<\/ul>\n<p>For example, if you wanted to search you .bash_profile for a literal dot (.) then using grep would be difficult because you would have to escape the dot because dot is a meta character that means &#8216;wild-card, any single character&#8217;:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">grep &quot;.&quot; myfile.txt<\/code><\/pre> <\/div>\n<h4 id=\"the-above-command-returns-every-line-of-myfile-txt-do-this-instead\"><span style=\"color: #ff6600;\"><strong>The above command returns every line of myfile.txt. Do this instead:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">fgrep &quot;.&quot; myfile.txt<\/code><\/pre> <\/div>\n<p>Then only the lines that have a literal &#8216;.&#8217; in them are returned. fgrep helps us not bother escaping our meta characters.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Grep: Grep (Global Regular Expression Print) is a command-line utility for searching plain-text data sets for lines matching a regular expression. &#8230; Grep was originally developed for the Unix operating system, but is available today for all Unix-like systems. egrep egrep is an acronym for &#8220;Extended Global Regular Expressions Print&#8221;. It is a program which [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699],"tags":[4081,4070,4082,4083,4069,4074,4086,4075,4085,4084,4072,4077,4068,4071,4079,4078,4080,4073,4076],"class_list":["post-1902","post","type-post","status-publish","format-standard","hentry","category-linux","tag-egrep","tag-egrep-empty-subexpression-when-attempting-to-filter-out-words-from-a-list","tag-and-fgrep","tag-difference-between-grep-and-egrep","tag-egrep-regular-expression-for-times-over-five-minutes","tag-fgrep","tag-fgrep-match-patterns-in-a-file","tag-find-and-grep-commands","tag-grep","tag-grep-and-egrep-regular-expressions","tag-grep-template-for-extracting-lines-where-second-word-has-only-two-vowels","tag-how-can-i-grep-for-a-string-containing-regex-metacharacters-like-and","tag-how-to-filter-content-between-specific-keywords-using-grep","tag-how-to-get-numbers-of-specified-range-of-length-from-string-with-grep","tag-how-to-use-restricted-characters-in-egrep","tag-using-grep-command-and-backreferencing","tag-what-is-the-difference-between-grep","tag-what-is-the-difference-between-egrep","tag-what-is-the-difference-between-grep-e-and-grep-e-option"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1902"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1902\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}