bash grep 区分大小写 [AZ]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11166825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:34:19  来源:igfitidea点击:

grep case sensitive [A-Z]?

bashcommand-linegrep

提问by Steven Penny

I cannot get grepto case sensitivesearch with this pattern

我无法使用此模式grep进行区分大小写的搜索

$ grep 'T[A-Z]' test.txt
The Quick Brown Fox Jumps Over The Lazy Dog
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG

采纳答案by Paused until further notice.

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

使用引号防止模式被 shell 匹配为文件系统中的文件。 ''

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

使用命名字符类来保证区分大小写的匹配。 [[: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.

[A-Z]对您不起作用的原因是您使用的语言环境在您的系统上实现的方式,该模式还包括小写字母。

回答by Steven Penny

You can set LANG value:

您可以设置 LANG 值:

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

回答by Nahuel Fouilleul

grep 'T[[:upper:]]' test.txt
grep 'T[ABCDEFGHIJKLMNOPQRSTUVWXYZ]' test.txt