BASH Programming

How Do You Grep Case Sensitive?

The “grep” command can be used in the Linux operating system as a means to deal with case sensitivity in any string. The “grep” command’s default behavior is case-sensitive. Users are given the option to browse a design among files using this technique. To make “grep” case-insensitive, users must have either the “—ignore-case flag” or the “-i” argument. The “grep” command must be used with the “-i flag” when looking for a gradient. When users are unclear about the situation, they choose to ignore it.

Utilization of “Grep” to Ignore Case

It is indeed frequently advantageous to be able to discard the case of the words that we are scanning with when utilizing “grep” to analyze a file. Grep is case-sensitive by nature. Typically, we might have been searching system logs with “grep”. In this article, we will have a deep discussion on the Linux command topic of “grep” for working on the case sensitive. To accomplish this, we will first create a file called “linux.txt” in the “Desktop” directory of our Linux operating system and store some text in it.

benstokes
Benstokes
BenStokes
BENSTOKES

 
After assigning it to the “linux.txt” file, we will save it on the desktop.


When the file is being created, we would open the terminal now for implementing the Linux commands. In this article, we will mostly utilize the “grep” command for understanding the case sensitivity phenomenon in a better way. When the terminal is opened, we will add up the first command of “grep” for attaining the case of small letters for the “benstokes” from the linux.txt file. We used the following command to attain the lowercase letter and provide that case very next to the “grep” command as provided in the following instruction.

Linux@linux:~/Desktop$ grep benstokes linux.txt

 
When we provide the command above in the Linux terminal and press the enter key it would display the alphabetical letter of the lowercase letter in the file present. This determines that it works along with the case insensitivity for the textual letters as the spelling remains the same: just having the difference of upper- or lower-case letters which makes it to read as four different keywords. This command would provide the output that is provided below.


On the screen, lowercase letters would appear. As we can see, it would only return a particular outcome that fits the search phrase in terms of the scenario. Fortunately, it is simple to make the “grep” search case insensitive. It would be represented to be notified by the “grep –i”. So, now, we will provide another command on our terminal that would notify all the cases on the screen without filtering it out by the search cases. The command that we provide next is given below.

Linux@linux:~/Desktop$ grep -i benstokes linux.txt

 
When we wrote this command on the terminal and then pressed the enter button to terminate the processing, it would display the data that is present in the “linux.txt” file and display it as provided in the below screenshot of the output display.


In that very same instance, since we prompted “grep” to neglect the case, we can view each variation of the search query that was discovered in the text file name “linux.txt” that is present in the desktop created just before the start of this command execution of Linux. This same phenomenon would also function flawlessly when the text file of “linux.txt” is passed from the command “cat”. The command is provided as:

Linux@linux:~/Desktop$ cat linux.txt | grep -i "benstokes"

 
If we do not need to bother about the case, the screenshot above will locate any items in linux.txt that include “benstokes” regardless of whether it’s “Benstokes”, “BenStokes”, or “BENSTOKES”. This makes it much simpler as of “benstokes”. As a variation, we might utilize it along with “cat” or by flipping it to “tac”. This would be exactly similar to what we would obtain in the previous command as well.

While executing the command, we will press enter to perform processing. It would be similar to the functionality and would ignore the case insensitivity rather than searching it out. It will pop out each of the characters of the cases on the output as we provided in the output display.


This is another way of using “grep” to ignore cases where the first is to introduce a file name present in the directory and then employ the -I command with “grep” instantly after the “|” operator. When used with the “|” operator, we must utilize the word “cat”.

The whole of the file data would be shown on the screen of the Linux terminal without filtering out or in any upper- or lower-case letters distinction. It stands for “–ignore-case” and is abbreviated as “-i” to make it easier for us to retain. Now, we will provide the next command of “grep” which will also provide a similar output to that with the previous two commands which do not follow any restriction for any case sensitivity. The command that we provide for the third time for showing the “grep” functionality is provided below.

Linux@linux:~/Desktop$ cat linux.txt | grep --ignore-case "benstokes"

 
Here, we provide the “grep” command along with the usage of the “cat” command on the “linux.txt” file with the conception of the functionality of “ignore-case” on the case of “benstokes”. So, it would probably display the file data of “linux.txt” without any exception or searching and popping out the specific one. This command would display similar data to the file assigned to it earlier. The case that we used is a lowercase letter but it would ignore that case due to the “cat” command with the “grep” ignore case so it would perform an insensitive case search by the utilization of “grep”. The output would remain the same in this case also which is given below.

Conclusion

The Linux command about the “grep” case sensitivity is covered in this article. Together with some linking text containing the word “cat,” we have used the four commands that we have built. While the other commands display all strings present in the file relevant to the value provided in the command by ignoring upper- or lower-case letters, the first command simply generated lowercase letters for the file, whereas the other commands determine the reading of all strings without having any case sensitivity rule.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.