Using “grep” in If Statement in Bash [4 Cases]

The grep command is a useful tool to search for a text pattern within files. Along with the if statement, it allows users to conditionally execute commands based on whether a particular pattern is found in a file or not. Thus a message can be printed on the terminal according to the search result. In this article, I will explore the process of using the grep command in the bash if statement in different situations. So without delay, let’s start!

4 Cases of Using the “grep” Command in Bash If Statement

The grep command can be used inside the if statement and look for a match inside a specified file irrespective of the casing. Here I have listed 4 cases of using the grep command inside the bash if statement:

1. Using “grep” in the If Statement

The grep command captures a certain substring from a big string according to the declaration. This can be used inside the if statement for searching a specific word inside a specific file. To do so  use the syntax inside the condition of the if statement: grep -q "$search_word" "$file_to_search"

Here is a practical demonstration of that:

#!/bin/bash

# Define the word to search for
search_word="Linux"

# Specify the file to search in
file_to_search="file1.txt"

# Use grep to search for the word in the file
if grep -q "$search_word" "$file_to_search"; then
echo "The word '$search_word' was found in $file_to_search."
fi
EXPLANATION

After assigning the value, the if grep -q "$search_word" "$file_to_search" command search for search_word inside the file_to_search and print a message with the help of the echo command according to the search result.

The Bash script has found the “Linux” inside the file1.txt file.The “Linux” word is found inside the file1.txt file.

2. Using “grep” in the If-Else Statement

The grep command with the -i option can look for the matching of a pattern inside a file irrespective of the casing. To achieve this, use the syntax inside the condition of the if statement: $(grep -ic “search_word” “file_to_search”)

Here is how to search a word inside a file irrespective of casing:

#!/bin/bash

if [ $(grep -ic "linux" file1.txt)  ]
then
echo "We have found Linux."
else
echo "We haven't found Linux."
fi
EXPLANATION

Here the $(grep -ic "linux" file1.txt) command looks for linux inside the file1.txt file irrespective of the casing. The -i option ignores case distinctions in both the linux and the “file1.txt” file. Finally prints a message according to the output.

The “linux” word has been found inside the file1.txt irrespective of the casing.The script has found the “linux” word inside the “file1.txt” irrespective of the casing.

3. Using “grep” in the Else-If Statement

The searching operation can be done using the grep command inside an else-if statement where the output of the grep command will be set as a condition of the else-if statement.

Here is the bash script of a combination of grep command and else-if statement:

#!/bin/bash

if [ $(grep -ic "Linux" file1.txt) -eq 1 ]
then
echo "We have found Linux."
elif [ $(grep -ic "Linux" file1.txt) -gt 1 ]
then
echo "Linux is there multiple times."
else
echo "We haven't found Linux."
fi
EXPLANATION

The first if [ $(grep -ic "Linux" file1.txt) -eq 1 ] command looks for the one-time match, if it is not true then the subsequent elif [ $(grep -ic "Linux" file1.txt) -gt 1 ] command looks for multiple matching. They subsequently print a message according to the scenario.

The word "Linux" exists multiple time inside the file1.txt.The script has found multiple matches.

4. Using “grep” in a Nested If Statement

A nested if statement means the usage of an if statement in a nested manner. Which means using the if statement inside the if statement. Programmers can utilize this feature to determine how many times a match is found.

Here is a practical bash script for this purpose:

#!/bin/bash

if [ $(grep -ic "Linux" file1.txt) ]
then
if [ $(grep -ic "Linux" file1.txt) -eq 1 ]
then
echo "We have found Linux."
elif [ $(grep -ic "Linux" file1.txt) -eq 2 ]
then
echo "Linux is mentioned 2 times."
else
echo "Linux is mentioned more than twice."
fi
else
echo "Linux is not mentioned here"
fi
EXPLANATION

Here the if [ $(grep -ic "Linux" file1.txt) ] command only looks for “Linux” inside the file1.txt file. In case of match is found, it checks for how many times “Linux” is found. Finally prints a message according to the match result.

The “Linux” word has been found more than twice inside the file1.txt file.The bash script has found “Linux” more than twice inside the file1.txt file.

Conclusion

The grep is a great tool that offers programmers a wide range of versatility to skim through a giant file and find the necessary information according to the match. After reading this article I believe you have become enough skillful to implement this tool and unleash your potential in Bash scripting. If you have any questions or queries about this article, feel free to comment below.

People Also Ask

How do I know if grep is successful?

If the grep command finds a pattern within the specified file, grep returns an exit status of 0, which means success. If the pattern is not found, the exit status returned is 1. The grep program can get its input from a standard input or a pipe, as well as from files.

Can I use grep in an if statement?

Yes, you can certainly use the grep command inside the condition of an if statement to find a specific pattern inside a file.

What are the flags for grep in bash?

There are four most useful and commonly known flags for the grep command. These are:

  1. -i: Enables case-insensitive search.
  2. -l: Lists only the names of matching files.
  3. -w: Matches whole words only.
  4. -v: Lists only the lines that do not match the pattern.

Why is grep useful?

The grep command is useful to search for matching patterns in a file. The full form of grep is global regular expression print. When a system admin wants to scrape through log files or a developer tries to find certain occurrences in the code file, then grep is the most perfect and powerful command tool to use.

Can grep count lines?

Yes, grep can count lines and occurrence of the specific pattern inside the target file. To achieve the result you have to use the syntax: count=$(grep -c "$search_line" "$file_to_search")

What algorithm does grep use?

The grep command uses the Boyer Moore string search algorithm. This algorithm is famous for being unbelievably efficient.

Related Articles


<< Go Back to If Else in Bash | Bash Conditional Statements | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment