Brief: In this beginner-friendly guide, we will discuss some practical examples of the fgrep command. By the end of this guide, users will be able to perform text search operations efficiently using the command line interface.
Text searching is one of the most commonly performed operations. However, this simple task quickly becomes time-consuming if users are not familiar with the correct tools. In Linux, there are various text-filtering utilities such as awk, sed, cut, etc.
However, in Linux, fgrep is the most preferred utility for simple text searching. In this guide, we are going to discuss some practice examples of the fgrep command that can be used in day-to-day life.
The fgrep command in Linux falls under the family of the grep command. However, it is used to search for the fixed string pattern instead of regular expressions. Hence the name of the command is fgrep (Fixed GREP).
The syntax of the fgrep command is similar to the other grep family commands:
$ fgrep [OPTIONS] PATTERNS [FILES]
To begin, let’s create a plain text file with the following contents to use an example:
$ cat input.txt
Here, we can see that the text file is ready with the sample contents. Now let’s discuss some common examples of the fgrep command in the next few examples.
1. How fgrep Is Different than grep and egrep Commands?
As the name suggests, the fgrep command is used to search for the fixed string patterns. It interprets the pattern as a fixed string instead of a regular expression. Hence it performs the search operation in a time-efficient way.
To understand the difference, let’s use a dot (.)
character with the grep command.
This simple regular expression matches any single character except for the end of the line:
$ grep ha. input.txt
In the above output, we can see that the dot (.)
character matched the text har, hat, and has.
Now, let’s use the same pattern with the fgrep command and observe the result:
$ fgrep ha. input.txt
In the above output, we can see that the command fails to find the given pattern.
This happens because the fgrep command doesn’t recognize regular expressions and tries to search for the non-existing pattern – “ha.”
.
[ You might also like: What’s Difference Between Grep, Egrep, and Fgrep in Linux? ]
2. How to Search for a Pattern in a File
Let’s start with the basic example where we will search for a string professional in an input.txt file:
$ fgrep professionals input.txt
As we can see, the pattern matching succeeds at two places and it’s highlighted in red color.
3. How to Set Grep Output Color for Matched Patterns
In the previous example, we saw that, by default, the matched pattern is highlighted in red color. However, we can alter this behavior by assigning a different value to the GREP_COLOR
environment variables.
Let’s assign the value 32 to the GREP_COLOR
environment variable to highlight matched pattern in green color:
$ export GREP_COLOR=32 $ fgrep professionals input.txt
Now, before moving to the next example, unset the GREP_COLOR environment variable to enable the default behavior:
$ unset GREP_COLOR
4. How to Search for Multiple Patterns in a File
Sometimes, we need to perform pattern matching for multiple strings. In such cases, we can provide the patterns from the text file instead of the command line argument.
Let’s create a text file that contains multiple patterns on a separate line:
$ cat pattern.txt professionals website
Now, let’s use this file with the -f
option for multiple pattern matching:
$ fgrep -f pattern.txt input.txt
In the above output, we can see that the pattern matching succeeds for the strings professionals and website.
5. How to Limit the Number of Matches in a File
By default, the fgrep command continues to perform the pattern matching until the entire file is processed.
However, sometimes we need to limit the number of matches. In such cases, we can use the -m
option with the command:
$ fgrep -m 1 professionals input.txt TecMint was started on 15th August 2012 by technical professionals and all the
In this example, the fgrep command stops the file processing after matching the first pattern.
6. How to Print File Name When Searching Pattern
Sometimes, we just need to find the name of the files in which a particular pattern is present. In such cases, we can use the -l
option of the fgrep command:
$ fgrep -l professionals input.txt input.txt
Here, we can see that the command just prints the file name instead of the lines with the matched patterns.
7. How to Print File Name When Pattern Matching Fails
In the previous example, we saw how to print the file name when pattern matching succeeds. Now, let’s see how to perform the operation in a reverse way.
Let’s try to find the non-existing pattern in the file and observe the result:
$ fgrep -L non-existing-word input.txt input.txt
In this example, we used the -L
option of the command that prints the file name when pattern matching doesn’t succeed.
8. How to Suppress Error Messages
Error handling plays a crucial role while writing shell scripts. However, in some non-critical scenarios, we can safely ignore the error messages.
In fgrep, we can use the -s
option that suppresses errors related to non-existing or unreadable files. To understand this behavior in a better way, let’s try to search for a pattern in the non-existing file:
$ fgrep -s professionals non-existing-file.txt $ echo $? 2
In the above output, we can see that the command doesn’t display any error on the standard error stream. However, the failure is reported by the non-zero return value.
In addition to this, we can also observe the same behavior when the file is unreadable. So, first, modify the file permission using the chmod command:
$ chmod 000 input.txt $ ls -l input.txt
Now, try to search for the pattern and observe the result:
$ fgrep -s professionals input.txt $ echo $?
9. How to Exclude Partially Matched Lines
In the previous examples, we saw that the fgrep command performs a partial match. However, in some cases, we need to perform the exact match for the entire line. In such cases, we can use the -x
option of the fgrep command.
$ fgrep -nx "the same site is sometimes hard to find." input.txt
In the above output, the exact line match happens at line number 20.
10. How to Exclude Files While Searching Recursively
Sometimes, we need to skip certain files while performing search operations. In such cases, we can use the --exclude
option of the fgrep command.
To understand this, first create a directory and copy a few sample files into it:
$ mkdir dir-1 $ cp input.txt dir-1/input.txt $ cp input.txt dir-1/input.data $ cp input.txt dir-1/input.md
Now, let’s search for the pattern in all files of the dir-1 directory except the “.txt”
files:
$ fgrep -r --exclude *.txt professionals dir-1
In the above output, we can see that pattern matching was performed on the files with “.md”
and “.data”
extensions.
11. How to Exclude Multiple File Types While Searching Recursively
In the previous example, we saw how to exclude files with particular extensions. However, the command becomes lengthy when we want to exclude files with multiple extensions. In such cases, we can provide the exclude pattern from the file instead of the command line argument:
So, let’s create a text file that contains multiple exclude patterns on a separate line:
$ cat skip-pattern.txt *.txt *.data
Now, let’s use this file with the --exclude-from
option:
$ fgrep -r --exclude-from skip-pattern.txt professionals dir-1
Here, we can see that pattern matching was performed on the files with “.md”
extensions only.
12. How to Include Files While Searching Recursively
In the previous examples, we saw how to exclude files while performing search operations recursively. Now, let’s see how to perform the operation in a reverse way.
Let’s use the --include
option of the fgrep command to perform a search on the files with the “*.txt”
extension:
$ fgrep -r --include "*.txt" professionals dir-1
In the above output, we can see that the pattern matching is skipped for the files with “*.md”
and “*.data”
extensions.
In this article, we discussed some useful examples of the fgrep command. Beginners can use these examples in day-to-day life to improve productivity while working with Linux.
Do you know of any other best example of the fgrep command in Linux? Let us know your views in the comments below.
When I put fgrep on the terminal, the answer is: “fgrep: warning: fgrep is obsolescent; using grep -F“.