While grep is a well-known Linux command (we’ve already covered it in detail), do you know that there exist two different variants of this tool? Yes, these are egrep and fgrep. In this tutorial, we will discuss the latter (fgrep) using easy to understand examples. But before we do that, it’s worth mentioning that all examples and instructions mentioned in this tutorial have been tested on Ubuntu 16.04LTS.
Linux fgrep command
If you take a look at the official documentation, the man page for the grep command says fgrep is nothing but grep -F. It also says variants like fgrep and egrep are only provided for backward compatibility. We’ll touch upon these details later in this tutorial.
Meanwhile, here’s the syntax of fgrep (which is same as that of grep):
grep [OPTIONS] PATTERN [FILE…]
And here’s how the grep command man page explains the tool:
grep searches the named input FILEs for lines containing a match to the given PATTERN. If no files are specified, or if the file “-” is given, grep searches standard input. By default, grep prints the matching lines.
The following Q&A-styled examples should give you a better idea on how fgrep works.
Q1. How is fgrep different from grep and egrep?
The fgrep command differs from both grep and egrep in the sense that it interprets PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines.
Since it’s said to be same as grep -F, here’s how an expert explained grep’s -F option over at StackExchange:
-Fswitch switches grep into a different mode where it accepts a pattern to match, but then splits that pattern up into one search string per line and does an OR search on any of the strings without doing any special pattern matching.
Q2. How to use fgrep?
To understand fgrep usage, let’s first take a basic example. Suppose there’s a file (file1) that contains the following contents:
And there’s another file (words.txt) that contains a list of words that we want to search in file1.
So here’s how you can use fgrep to perform this search:
fgrep -f words.txt file1
So you can see the words that were found were highlighted in red. Now some of you may argue that’s what grep and egrep would also do in this case, then how fgrep differs?
Firstly, yes, you are right. The grep and egrep command would also produce similar output:
But the difference will be clear in case file1 and words.txt contain the following contents (observe special characters within words):
But fgrep will work as intended:
Q3. Is there really no difference between fgrep and grep?
Going by the official documentation, there isn’t any difference between fgrep and the grep -F command. However, there’s reportedly one major difference between the two tools: the string matching algorithm they use. While fgrep uses the Aho-Corasick algorithm, grep makes use of a modified version of Commentz-Walter.
“This means that
grep has a worst case O(mn) complexity while
fgrep is at worst O(m+n),” says an expert on StackExchange forums.
As evident, the fgrep command isn’t difficult to understand and use. As for difference between fgrep and grep -F, while we’ve already noted the points in Q3, we think this issue shouldn’t bother a large section of users. In case you are in doubt, or have a fgrep-related query, share that with us by leaving a comment below.