Open In App

fgrep command in Linux with examples

Last Updated : 11 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.


Previous Article
Next Article

Similar Reads

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 i
2 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
ZIP command in Linux with examples
ZIP is a compression and file packaging utility for Unix. Each file is stored in a single .zip {.zip-filename} file with the extension .zip. Zip is used to compress files to reduce file size and is also used as a file package utility. Zip is available in many operating systems like Unix, Linux, windows, etc.If you have limited bandwidth between two
7 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read