Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Difference between grep and fgrep command

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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
Last Updated : 21 Feb, 2023
Like Article
Save Article
Similar Reads