Difference between grep and fgrep command

The grep filter searches a file for a particular pattern of characters and displays all lines that contain that pattern. The fgrep filter searches for fixed-character strings in a file or files.

Syntax of grep command:

grep [options] pattern [files]

Syntax of fgrep command:



fgrep [options] pattern [files]

The main difference between both commands is:

  • String matching algorithm used by them.
  • fgrep always uses the Aho-Corasick algorithm that worst O(m+n) complexity.
  • grep command always uses the modified version of Commentz-Walter algorithm which has worst case O(mn) complexity.
  • fgrep command interprets the PATTERN as a list of fixed strings separated by newlines. But grep always interpreted as regular expressions.

Similarity between both the commands

Consider the below file named as para2

Hi, are you using geeksforgeeks for learning computer science concepts.
Geeksforgeeks is best for learning.

Consider the below Words :

are
using
geeksforgeeks
learning
concepts

Using grep Command:

$grep -f word para

Output:

Hi, are you using geeksforgeeks for learning computer science concepts.
Geeksforgeeks is best for learning.

Using fgrep Command:

$fgrep -f word para

Output:


Hi, are you using geeksforgeeks for learning computer science concepts.
Geeksforgeeks is best for learning.

Difference between both the commands

Consider the below File :

Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.
Geeks*forgeeks is best for learni\ng.

Consider the below Words :

@re
usin.g
geeks*forgeeks
learni\ng
con/cepts

Using grep Command:

grep -f word para

Output:

Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.

Using fgrep Command:

fgrep -f word para

Output:

Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.
Geeks*forgeeks is best for learni\ng.



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.