Differences between grep, fgrep, egrep…

Grep is a very useful tool in the Unix world. If you don’t know it already, it is very much like a search tool. It can search for a text or pattern in one or multiple input files or data coming from (unix) pipes.

Most of the Linux distributions out there offer three ways to use Grep from the command-line (aka terminal): grep, fgrep, and egrep.

grep

The grep command is the most common one but also the oldest. It offers Basic Regular Expression matching.
The particularity of BRE is that some metacharacters ({, }, and parenthesis) need to be escaped (using a backslash \) to be used properly. (see also egrep below)

grep 'testi\{1,2\}ng' file.txt

will match any lines containing either testing or testiing in file.txt.

fgrep

fgrep, short for fixed(-size strings) grep, is used to do a search for a string with a fixed size (can be seen as a constant), contrarely to patterns.

fgrep '{test}match*' file.txt

will match any text containing exactly {test}match*. No parsing or interpreting of regular expression or whatever, is done by grep.

egrep

egrep, short for extended grep, (contrarely to the simple grep) search for matches using a pattern written using Extended Regular Expressions. The changes from BRE is that you do not need to escape metacharacters, and also ERE provides three more metacharacters: |, +, and ? (note that they can still be used in the basic grep by escaping them).

egrep 'testi\{1,2\}ngs?' file.txt

will match any text containing any of: testing, testings, testiing, testiings.

2 Replies to “Differences between grep, fgrep, egrep…”

Leave a Reply