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.
are using geeksforgeeks learning concepts
$grep -f word para
Output:
Hi, are you using geeksforgeeks for learning computer science concepts. Geeksforgeeks is best for learning.
$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.
@re usin.g geeks*forgeeks learni\ng con/cepts
grep -f word para
Output:
Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.
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.