grep vs egrep vs fgrep

The grep command usually searches for a specified pattern match (character string sequence) within a text file; or series of text files.

There are three commands that are part of the grep family of commands:

grep, egrep, fgrep

fgrep (Fast grep): This command searches files for one or more pattern arguments. This command doesn’t understand the extended capabilities of regular expressions,  instead, it does direct string comparison to find matching lines of text in the input. . Used to find fixed strings. Very fast in it’s search.

grep: The commonly used  grep command. Accepts a subset of regular expressions. Pattern matching will be equivalent in speed until specific subsets of regular expressions are used

egrep  (Extended grep): command accepts the full set of the regular expression capabilities. Will run slower when complex pattern matching sequences are used.  If you include special characters in patterns typed on the command line, escape them by enclosing them in apostrophes to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to egrep, put a backslash (\) in front of the character. It is usually simpler to use fgrep when you don’t need special pattern matching.

This entry was posted in Linux. Bookmark the permalink.

One Response to grep vs egrep vs fgrep

  1. RussDraper says:

    I was always curious why some people prefer fgrep, now I know! Thanks for the article, it saved me from having to read the manual 🙂 I was using grep to sort passwd and shadow files to push out with puppet (posted in case someone else was searching for this as well):
    grep -xvf passwd1 passwd2
    grep -xvf passwd2 passwd1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s