{"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. \u2026 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 \u201cExtended Global Regular Expressions Print\u201d. 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 <flags> \u2018<regular expression>\u2019 <filename><\/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 $, *, [, \u2026 , (, ), and \\ characters are interpreted literally by the fgrep command.<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\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 \u201cGlobal Regular Expressions Print\u201d. 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 \u201czero or more of the preceding element\u201d.<\/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[pastacode lang=\u201dbash\u201d manual=\u201dgrep%20%3Cflags%3E%20\u2019%3Cregular%20expression%3E\u2019%20%3Cfilename%3E%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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>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\u2019t 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 \u201cExtended Global Regular Expressions Print\u201d.<\/li>\n<li>The \u2018E\u2019 in egrep means treat the pattern as a regular expression. \u201cExtended Regular Expressions\u201d abbreviated \u2018ERE\u2019 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[pastacode lang=\u201dbash\u201d manual=\u201dgrep%20%22%2B%22%20myfile.txt%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>egrep on the other hand treats the \u201c+\u201d as a meta character and returns every line because plus is interpreted as \u201cone or more times\u201d.<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201degrep%20%22%2B%22%20myfile.txt%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 \u201cFixed-string Global Regular Expressions Print\u201d.<\/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 \u2018wild-card, any single character\u2019:<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dgrep%20%22.%22%20myfile.txt%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dfgrep%20%22.%22%20myfile.txt%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Then only the lines that have a literal \u2018.\u2019 in them are returned. fgrep helps us not bother escaping our meta characters.<\/p>\n[ad type=\u201dbanner\u201d]\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. \u2026 Grep was originally developed for the Unix operating system, but is available today for all Unix-like systems. egrep egrep is an acronym for \u201cExtended Global Regular Expressions Print\u201d. 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}]}}