How to Use fgrep Command to Search for Strings in Linux


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
View File Contents in Linux
View File Contents in Linux

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
Searching for Single Character with Grep
Searching for Single Character with Grep

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
Search for Pattern in File
Search for Pattern in File

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
Set Grep Output Color for Matching Patterns
Set Grep Output Color for Matching Patterns

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
Search for Multiple Patterns with fgrep
Search for Multiple Patterns with fgrep

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 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

Jassweb 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 $?
Grep Ignore Error Messages
Grep Ignore Error Messages

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.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?