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 a file is grep utility. It is very vast in functionality which can be attributed to a large number of options it supports like: searching using string pattern, or reg-ex pattern or perl based reg-ex etc.

 

Due its varying functionalities, it has many variants including grep, egrep (Extended GREP), fgrep (Fixed GREP), pgrep (Process GREP), rgrep (Recursive GREP) etc. But these variants have minor differences to original grep which 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

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

 

Differences Between grep, egrep and fgrep

 

Some main differences between grep, egrep 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:

 

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.

$ grep -C 0 '(f|g)ile' check_file
$ grep -C 0 '\(f\|g\)ile' check_file

 

Like here, when the command is run without escaping '(' ')' and '|' then it searched for the complete string i.e. “(f|g)ile” in 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 “file” or “gile” in the file.

 

Egrep Command

 

Egrep or grep -E is 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’t 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.

 

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.

$ egrep -C 0 '(f|g)ile' check_file
$ egrep -C 0 '\(f\|g\)ile' check_file

 

Like here, egrep searched for “file” string 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 “(f|g)ile” in the file.

 

fgrep Command

 

Fgrep or the Fixed grep or grep -F is 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’t recognize the regular expressions, neither any meta-characters. For searching any direct string, this is the version of grep which should be selected.

 

Fgrep searches for complete string and doesn’t even recognize special characters as part of regular expression even if escaped or not escaped.

$ fgrep -C 0 '(f|g)ile' check_file
$ fgrep -C 0 '\(f\|g\)ile' check_file

 

Like, when meta-characters were not escaped, fgrep searched for the complete string “(f|g)ile” in the file, and when the meta-characters were escaped, then the fgrep command searched for “\(f\|g\)ile” all characters as is in the file.

 

Conclusion

 

Above highlighted are the differences between ‘grep’, ‘egrep’ and ‘fgrep’. 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 “egrep” or “fgrep”, “grep -E” or “grep -F” are recommended to be used.

這篇文章有幫助嗎? 0 Users Found This Useful (0 Votes)