grep case sensitive [A-Z]?

15,905

Solution 1

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.

Solution 2

You can set LANG value:

$ LANG=C grep 'T[A-Z]' test.txt
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG

Solution 3

grep 'T[[:upper:]]' test.txt
grep 'T[ABCDEFGHIJKLMNOPQRSTUVWXYZ]' test.txt
Share:
15,905

Related videos on Youtube

Zombo
Author by

Zombo

Hello world

Updated on September 15, 2022

Comments

  • Zombo
    Zombo about 1 year

    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
    
  • SourceSeeker
    SourceSeeker over 11 years
    @svnpenn: See this and this for a discussion of the issues.