Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep case sensitive [A-Z]?

I cannot get grep to case sensitive search with this pattern

$ grep 'T[A-Z]' test.txt
The Quick Brown Fox Jumps Over The Lazy Dog
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
like image 831
Zombo Avatar asked Jun 23 '12 04:06

Zombo


People also ask

Is grep case sensitive or case sensitive in Linux?

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. We will use the following grep command which is case sensitive by default. The output is like below.

How do I grep in case insensitive match?

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

What are the features of grep?

The simplest feature of grep is to handle case sensitivity. Grep is case-sensitive by default hence it shows the perceptibility of both upper and lower cases in the file.

What is the use of grep command?

The simplest feature of grep is to handle case sensitivity. Grep is case-sensitive by default hence it shows the perceptibility of both upper and lower cases in the file. This feature helps in getting the required output by removing the discrimination of the case which can all be done on the main page of grep.


2 Answers

Use quotes to prevent the pattern from being matched as a glob to file(s) in the filesystem by the shell. ''

Use a named character class to guarantee a case-sensitive match. [[:lower:]]

Use a quantifier to make matches for more than one character. \+

Use anchor(s) to make sure the match is positioned properly. ^

grep '^T[[:upper:]]\+' test.txt

The reason that [A-Z] isn't working for you is that the way the locale you're using is implemented on your system, that pattern also includes lowercase letters.

like image 182
Dennis Williamson Avatar answered Oct 19 '22 14:10

Dennis Williamson


You can set LANG value:

$ LANG=C grep 'T[A-Z]' test.txt
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
like image 44
Zombo Avatar answered Oct 19 '22 15:10

Zombo