fgrep command in Linux with examples

11 Oct, 2021
The fgrep filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings which contain lots of regular expression metacharacters, such as “^”, “$”, etc.
Syntax:
fgrep [options] [ -e pattern_list] [pattern] [file]

Options with Description:
-c : It is used to print only a count of the lines which contain the pattern.
-h : Used to display the matched lines.
-i : During comparisons, it will ignore upper/lower case distinction.
-l : Used to print the names of files with matching lines once, separated by new-lines. It will not repeat the names of files when the pattern is found more than once.
-n : It is used precede each line by its line number in the file (first line is 1).
-s : It will only display the error messages.
-v : Print all lines except those contain the pattern.
-x : Print only lines matched entirely.
-e pattern_list : Search for a string in pattern-list (useful when the string begins with a “-“).
-f pattern-file : Take the list of patterns from pattern-file.
pattern : Specify a pattern to be used during the search for input.
file : A path name of a file to be searched for the patterns. If no file operands are specified, the standard input will be used.
Below are the examples with options to illustrate the fgrep command:
Consider below file as input. Here it is create using cat command and “name of the file is para”.
Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.Geeks*forgeeks is best for learni\ng.
-c option: Displaying the count of number of matches. We can find the number of lines that match the given string.
Example:
$fgrep -c "usin.g" para
Output:
1
-h option: To display the matched lines.
Example:
 fgrep -h "usin.g" para
Output:
Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.

-i option: Used in case insensitive search. It ignore upper/lower case distinction during comparisons. It matches words like : “geeks*forgeeks”, “Geeks*forgeeks”.
Example:
 fgrep -i "geeks*forgeeks" para
Output:
Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.
Geeks*forgeeks is best for learni\ng.

-l option: It will display the file names that match the pattern. We can just display the files that contains the given string/pattern.
Example:
fgrep -l "geeks*forgeeks" para para2
Output:
para
-n option: Precede each line by its line number in the file. It shows line number of file with the line matched.
Example:
$ fgrep -n "learni\ng" para
Output:
1:Hi, @re you usin.g geeks*forgeeks for learni\ng computer science con/cepts.
2:Geeks*forgeeks is best for learni\ng.

-v option: It is used to display all lines except those that contain the pattern. It will print all lines except those that contain the pattern.
Example:
fgrep -v "@re" para
Output:
Geeks*forgeeks is best for learni\ng.

-x option: It will display only lines matched entirely.
Example 1:
fgrep -x "@re" para
Output:
 
Example 2:
fgrep -x "Geeks*forgeeks is best for learni\ng." para
Output:
Geeks*forgeeks is best for learni\ng.

anikaseth98
linux-command
Linux-text-processing-commands
Picked
Technical Scripter 2018
Linux-Unix
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
scp command in Linux with Examples
chown command in Linux with Examples
SED command in Linux | Set 2
nohup Command in Linux with Examples
Introduction to Linux Operating System
Array Basics in Shell Scripting | Set 1
chmod command in Linux with examples
mv command in Linux with examples
Basic Operators in Shell Scripting