How to Ignore case using grep – Insensitive String.

Introduction

How to ignore case using grep command in Linux image only

In this article, I will discuss “How to Ignore case using grep”. Ignoring the case means the word can be in a lower letter or start in capital letters.

Suppose we have a text file with the words “Tastethelinux”, tastethelinux, and “tastetheLinux”. So there are 3 ways to write the same word.

Also, we have this word in or text file and we have to ignore capital and small letters. The grep command, can search a whole line, exact match, exclude the pattern and search multiple words.

If you want to learn more about the grep command you can refer to the given link. So let’s discuss the 3 topics for the grep command to ignore case.

  1. grep command is case sensitive
  2. How to ignore case in grep command
  3. How to configure the permanent solution to ignore cases by using the grep command.

grep command is case sensitive.

As we all know grep command is already case sensitive, let’s understand with the examples.

[email protected]:~$ cat grep_example.txt 
1. TastetheLinux
2. tastethelinux
3. TASTETHELINUX
4. tasteTheLinux
5. TasteTheLinux
tastethel[email protected]:~$ 

So we have 5 different word cases but the spell is the same. Now let’s suppose we have to search the word tastethelinux.

[email protected]:~$ cat grep_example.txt |grep tastethelinux
2. tastethelinux
[email protected]:~$

So this only prints the 2nd line, which is the exact word match. By default, the behaviour of the grep command is case sensitive.


How to ignore case in grep command using -i option.

So we are expecting the output which spells “tastethelinux”. Let’s use -i or –ignore-case option with the grep command.

[email protected]:~$ cat grep_example.txt |grep -i tastethelinux
1. TastetheLinux
2. tastethelinux
3. TASTETHELINUX
4. tasteTheLinux
5. TasteTheLinux
[email protected]:~$ 

We got the 5 lines after ignoring the case as an output. You can also use –the ignore-case option for the same output.


How to configure the permanent solution to ignore cases by using the grep command.

Suppose when we use the grep command it always ignores the case. For that, we have to make the entry into the .bashrc file.

$ echo "alias grep=grep -i" >> ~/.bashrc

The above command will create an alias for the grep command. when you use the grep command by default it will execute the grep -i command. And we have made the entry into the .bashrc file which is in the home directory of the user.

NOTE: When you make an entry into the .bashrc file, then open a new tab and try the command.

[email protected]:~$ cat grep_example.txt |grep tastethelinux
1. TastetheLinux
2. tastethelinux
3. TASTETHELINUX
4. tasteTheLinux
5. TasteTheLinux
[email protected]:~$ 

So we have used the grep command without the -i option, but we are getting the required output.

If you want to learn more about the grep command, you can refer to the man page of this.


Give your valuable time