How to Make Grep Case Insensitive (2024)

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:

  • “COMMAND LINE”
  • “Command line”
  • “CoMMand LiNe”

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.

See command and expected output here

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].

How to Make Grep Case Insensitive (1)

See command and expected output here

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

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

How to Make Grep Case Insensitive (2)

See command and expected output here

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.

See command and expected output here

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.

How to Make Grep Case Insensitive (2024)

FAQs

How to Make Grep Case Insensitive? ›

To make grep case insensitive, all you have to do is add an -i or —ignore-case flag. As always, you can type man grep 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 grep command.

How to make regex case-insensitive? ›

By default, regexpi performs case-insensitive matching. Use the regexp function with the same syntax as regexpi to perform case-sensitive matching. To disable case-sensitive matching for regexp , use the 'ignorecase' option.

Does Grepl ignore the case? ›

Syntax of the grepl function

The “ignore. case” argument is a logical value indicating whether the pattern matching should be case-insensitive. Its default value is FALSE. The “perl” argument is also a logical value indicating whether the pattern should be treated as a Perl-compatible regular expression.

How to do a case-insensitive search in Linux? ›

But remember the -name flag performs a case-sensitive search. If you are looking to do a case-insensitive search, you can use the -iname flag instead. You can also use the find command as an alternative to the ls command in some places. Let's assume you need to find all the files ending with the .

How do you find case-insensitive in less? ›

Note: By default, searching in less is case-sensitive. Ignore case sensitivity by specifying the -I option or pressing the I key within less .

How to grep case-insensitive? ›

To make grep case insensitive, all you have to do is add an -i or —ignore-case flag. As always, you can type man grep into your command line to get the official documentation for grep and all its flags and parameters.

What is the string method for case-insensitive? ›

Java String equalsIgnoreCase() Method

The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not.

What is the difference between grep and Grepl? ›

The grep and grepl functions use regular expressions or literal values as patterns to conduct pattern matching on a character vector. The grep returns indices of matched items or matched items themselves while grepl returns a logical vector with TRUE to represent a match and FALSE otherwise.

What is grepl R? ›

Overview. The word “grepl” stands for “grep logical”. The grepl() function in R simply searches for matches in characters or sequences of characters present in a given string.

What is the alternative to Grepl in R? ›

str_detect() is essentially equivalent grepl() . str_extract() plays the role of regexpr() and regmatches() , extracting the matches from the output. Finally, str_match() does the job of regexec() by provide a matrix containing the parenthesized sub-expressions.

Is grep Linux case sensitive? ›

Grep search is case sensitive by default. If you want the search to be case insensitive you can use -i option. In the preceding example, we searched for the word "LINUX" and grep returns the words Linux and LINUX as matching.

How to disable case sensitive in Linux? ›

When we open a shell, it runs the commands in this file. If we want to disable case sensitivity only in Bash, we can change it in . bashrc. The bind command changes Readline keybindings and configurations.

How do I make case-insensitive in Bash? ›

How can we then perform a case-insensitive match? One way is to convert all the variable values to all lowercase (or all uppercase) before doing the comparison. Let's apply this by converting the values of string1 and string2 variables to all lowercase. We can do so using the "${variable,,}" syntax.

How do I ignore case-sensitive in Unix? ›

Case-insensitive file searching with the find command

If you want to search for files and directories, leave that option off. The key to that case-insensitive search is the use of the -iname option, which is only one character different from the -name option. The -iname option is what makes the search case-insensitive.

How to use the grep command? ›

grep syntax
  1. -i: Prints lines with matching criteria while ignores casing (Upper/Lowecase).
  2. -l: Prints filenames only.
  3. -n: Prints lines with matching criteria and line numbers.
  4. -c: Prints count of lines with matching criteria.
  5. -v: Prints lines not matching criteria (inverse search).
  6. -w: Prints whole word matches.

How do you make a case-insensitive code? ›

You can achieve this using the toLowerCase() or toUpperCase() method in JavaScript. This code will output "The strings are equal!" because we're converting both string1 and string2 to lowercase before comparing them. By converting them to lowercase, we've made the comparison case-insensitive.

Which regex modifier performs case-insensitive matching? ›

IgnoreCase i

What is \b regex? ›

The small letter \b word boundary indicates that a pattern is bounded by a non-word character. Non-word characters are all characters apart from numbers, letters, and underscore ( _ ). They are denoted by another metacharacter ( \W ).

How to use case-insensitive in regex in Java? ›

In Java, by default, the regular expression (regex) matching is case sensitive. To enable the regex case insensitive matching, add (?) prefix or enable the case insensitive flag directly in the Pattern. compile() .

Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5793

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.