Home Linux How to Ignore Case when using Grep

How to Ignore Case when using Grep

by admin

When searching a file using grep it’s often useful to be able to ignore the case of the text you are searching for. By default grep is case sensitive. Typically you might be using grep to search log files, but lets use the following text file as an example:

buildvirtual
Buildvirtual
BuildVirtual
BUILDVIRTUAL

If we use grep to search for ‘buildvirtual’ we will get the following:

grep buildvirtual test.txt
buildvirtual

As you can see it has returned the single matching result, which matched the case used in the search term/pattern. Luckily it is straight forward to make the grep search case insensitive, by using the grep -i option:

grep -i buildvirtual test.txt
buildvirtual
Buildvirtual
BuildVirtual
BUILDVIRTUAL

This time we can see all variants of the search term found in the text file, because we have told grep to ignore case. This also works just fine if you are passing the text file from cat:

cat test.txt | grep -i "buildvirtual"

To help you remember that it’s the -i option, it’s short for --ignore-case:

cat test.txt | grep --ignore-case "buildvirtual"
buildvirtual
Buildvirtual
BuildVirtual
BUILDVIRTUAL

Summary

In this short tutorial you have seen how to ignore case or perform a case insensitive search using grep.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More