Menu Close

fgrep and egrep in Linux Finding patterns in Files… FTC


The fgrep command is similar to grep, but with three main differences: You can use it to search for several targets at once, it does not allow you to use regular expressions to search for patterns, and it is faster than grep. When you need to search many files or a very large file, the difference in speed can be significant.

With fgrep, you can search for lines containing any one of several targets. For example, the following command finds all entries in the phone numbers ftc file that contain any of the words “saul”, “michelle”, or “Walter“:

$  fgrep  "saul
>  michelle
>  walter"  ftc.txt

walter  White   555–2222
saul  (home)   555–1145
michelle   555–3244

When you give fgrep multiple search targets, each one must be on a separate line, and the entire search string must be in quotation marks. In this example, if you didn’t put michelle on a separate line you would be searching for saul michelle, and if you left out the quotes, the command would execute as soon as you hit ENTER.

With the −f (file) option, you can tell fgrep to take the search targets from a file, rather than having to enter them directly If you had a file in your home directory named .friends containing the usernames of your friends on the system, you could use fgrep to search the output of the finger command for the names on your list, like this:

$  finger | fgrep −f −/.friends



The egrep command is the most powerful member of the grep command family You can use it like fgrep to search for multiple targets, and it provides a larger set of regular expressions than grep.

In fact, if you find yourself using the extended features of egrep often, you may want to add an alias that replaces grep with egrep in your shell configuration file. (For example, if you are using bash, you could add the line “alias grep=egrep” to your .bashrc .)

You can tell egrep to search for several targets in two ways: by putting them on separate lines as in fgrep, or by separating them with the vertical bar or pipe symbol ( | ). For example, the following command uses the pipe symbol to tell egrep to search for the words dan, robin, ben, and walter in the file ftc.txt (phonelist) :

$  egrep "dan|robin|ben|walter" ftc.txt

walter  White   555–2222
dan  dnidz  x1234
robin  rpelc  x3141
ben  bsquared  x9876

Note that there are no spaces between the pipe symbol and the targets. If there were, egrep would consider the spaces part of the target string. Also note the use of quotation marks to prevent the shell from interpreting the pipe symbol as an instruction to create a pipeline.


Additional egrep Regular Expressions

+Matches one or more repetitions of the previous item..+any non-empty line
?Matches the previous item zero or one times.index\.html?index.htm, index.html
( )Groups a portion of the pattern.script(\.pl)?script,
|Matches either the value before or after the |.(E|e)xitExit, exit

The egrep command provides most of the basic options of both grep and fgrep.
You can tell it to ignore uppercase and lowercase distinctions (−i),
search recursively through subdirectories (−r),
print the line number of each match (−n),
print only the names of files containing target lines (−l),
print lines that do not contain the target (−v),
and take the list of targets from a file (−f).

Addition Regular Expressions for references

.Matches any single, thank, thunk,etc.
\Quotes the following character.script\
*Previous item may occur zero or more times in a row.ap*leale, apple, etc.
[a-z]Matches any one of the characters in the
[0–9]*any number: 0110, 27,
9876, etc.
$Anchor the pattern to the end of the string.\.$any string ending in a period
^Anchor the pattern to the beginning of a string.^ Ifany string beginning with If
{n,m}Previous item must occur at least n times but no more than m times.\*{3,5}***, ****,*****
[ ]Matches any one of the characters inside. Frequently used with ranges.[QqXx]*Q, q, X, or x
[^]Matches any character not inside the brackets.[^AZaz]any nonalphabetic character, such as 2