Pages

Thursday, June 29, 2017

What’s Difference Between Grep, Egrep and Fgrep in Linux?

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.
Difference Between grep, egrep and fgrep
Difference Between grep, egrep and fgrep in Linux
Due its varying functionalities, it has many variants including grepegrep (Extended GREP), fgrep (Fixed GREP), pgrep (Process GREP), rgrep (Recursive GREP) etc. But these variants have minor differences to original grepwhich has made them popular and to be used by various Linux programmers for specific tasks.
Main thing that remains to be investigated is what are the differences between the three main variants i.e. ‘grep’‘egrep’ and ‘fgrep’ of grep that makes Linux users choose one or the other version as per requirement.

Some Special Meta-Characters of grep

  1. + – Equivalent to one or more occurrences of previous character.
  2. ? – This denotes almost 1 repetition of previous character. Like: a? Would match ‘a’ or ‘aa’.
  3. ( – Start of alternation expression.
  4. ) – End of alternation expression.
  5. | – Matching either of the expression separated by '|'. Like: “(a|b)cde” would match either ‘abcde’ or ‘bbcde’.
  6. { – This meta-character indicates start of range specifier. Like: “a{2}” matches “aa” in file i.e. a 2 times.
  7. } – This meta-character indicates end of range specifier.

Differences Between grep, egrep and fgrep

Some main differences between grepegrep and fgrep can be highlighted as follows. For this set of examples we are assuming the file on which operation is being performed to be:
Linux grep Command
Linux grep Command

Grep Command

grep or Global Regular Expression Print is 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.
It uses Basic Regular Expressions apart from normal strings as a search pattern. In Basic Regular Expressions (BRE), meta-characters like: '{','}','(',')','|','+','?' loose their meaning and are treated as normal characters of string and need to be escaped if they are to be treated as special characters.
Also, grep uses Boyer-Moore algorithm for fast searching any string or regular expression.
      ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
                                                    ► Read more: http://adf.ly/1nBhHf
      ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

No comments:

Post a Comment