Linux grep case insensitive

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.

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.

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.

Читайте также:  Intel gigabit ethernet linux

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:

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.

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.

Читайте также:  Linux узнать размер блока файловой системы

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.

Источник

Grep Search Case Insensitive String – Ignore Case

Grep Search Case Insensitive String - Ignore Case

The grep tool is used to search and match the specified Patterns in a file or string. While searching and matching the upper case and lower case letters differ and do not match the upper case pattern with the lower case string. This is called case sensitivity. But the case sensitivity can be disabled and grep can search case insensitive in a string.

Grep Case Sensitive

The default behavior of the grep command is case sensitive. Case sensitive accepts the lower case different from uppercase. For example, the pattern “LINUX” doesn’t matches with the “linux” or “Linux” or “LinuX” etc. The text file is like below.

Linux LINUX LinuX L1nux 1LiNuX

We will use the following grep command which is case sensitive by default.

Grep Case Insensitive with -i Option

The case insensitive search can be made with the -i option for the grep command. The search pattern “LINUX” matches with “Linux”, “LiNux” or “LinuX” for the case insensitive search.

The –ignore-case is the long-form version of the -i option. So we can use the –ignore-case for case insensitive match with the grep command too.

grep --ignore-case "LINUX" file.txt

Grep Case Insensitive with Other Command Output

Alternatively, another command output can be also grepped with the grep command. This command output can be grepped in a case insensitive manner by using the -i option. In the following example, we will grep case insensitive the cat command output. We will also use the pipe operator to redirect cat command output to the grep command.

cat file.txt | grep -i "LINUX"

Grep Permanent Case Insensitive Configuration

The case insensitive math with the grep command can be made permanent. Even there are different ways the most practical and easy way is using a bash alias. We will create a bash alias named “grep” which refers to the “grep -i”. Every time the grep command is called the case insensitive option added by default. We will just add the alias like below.

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

If you want to make a case-sensitive search with this permanent case insensitive configuration you can use the –no-ignore-case option which makes the match is case sensitive.

grep --no-ignore-case "LINUX" file.txt

Источник

Читайте также:  Android studio linux uninstall

How to Make Grep Case Insensitive

Grep is a command used to find words in a string or file. By default, grep is case sensitive. That means all of these search terms would be treated differently:

To clarify, if my file contained the string “command line”, none of the above searches would return anything.

Grep Ignore Case

You can add an [.inline-code]-i[.inline-code] or [.inline-code]–ignore-case[.inline-code] flag to make grep case sensitive:

[.inline-code]grep -i “command line” textfile.txt[.inline-code]

[.inline-code]grep –ignore-case “command line” textfile.txt[.inline-code]

Running Through an Example

1. Run the following in your terminal:

 echo "1) Grep stands for 'global regular expression print'. 2) grep searches one or more input files for lines that match a given pattern. 3) GREP is a Linux / Unix command line tool. 4) gReP originated in the 1970’s. 5) grep can also be used as a verb - 'I grepped the logs for the words 'error' and 'warning''. 6) Here is a random line of text that does not mention g r e p at all." > grepFacts.txt

This command will create a file called [.inline-code]grepFacts.txt[.inline-code], put the 6 facts of grep that we’ve written out for you into that file, and save it in your current directory.

2. [Feel Free To Skip This Step] If you’re curious or want to verify that this command is actually saved in your file, run [.inline-code]cat grepFacts.txt[.inline-code]. This command will output all the contents of the file [.inline-code]grepFacts.txt[.inline-code].

3. Run [.inline-code]grep “grep” grepFacts.txt[.inline-code].

As you can see, this command only matches with the lowercase version of “grep”.

4. Now, run [.inline-code]grep -i “grep” grepFacts.txt[.inline-code].

As you can see, this command matches with all instances of “grep”, regardless of case.

Conclusion

To recap, the [.inline-code]grep[.inline-code] command allows you to search for a pattern inside of files, and is case sensitive by default. To make grep case insensitive, all you have to do is add an [.inline-code]-i[.inline-code] or [.inline-code]—ignore-case flag[.inline-code].

As always, you can type [.inline-code]man grep[.inline-code] into your command line to get the official documentation for grep and all its flags and parameters. If you want to learn more, check out this page for more information on the [.inline-code]grep[.inline-code] command.

Источник

Оцените статью
Adblock
detector