bash 如何使用grep查找仅包含小写的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23361229/
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 10:20:12  来源:igfitidea点击:

How to find lines that contain ONLY lowercase using grep

regexbashgrep

提问by Roosh

I am new to bash and am learning to use grep.

我是 bash 新手,正在学习使用 grep。

grep ^[a-z] file.txtwill show all the lines that begin with lowercase
grep [a-z] file.txtall lines with lowercase

grep ^[a-z] file.txt将显示所有以小写开头的行
grep [a-z] file.txt所有以小写开头的行

Can't figure out how to show lines with ALL lowercase, can anyone help?

无法弄清楚如何用所有小写字母显示行,有人可以帮忙吗?

回答by anubhava

You can use anchors in your regex for egrep:

您可以在正则表达式中使用锚点egrep

egrep '^[[:lower:]]+$' file

This egrepwill only find lines that have only lowercase lettersin the (not even space is allowed).

egrep只会找到只有小写字母的行(甚至不允许空格)。

回答by Unknown

This will match and exclude lines that contain something else besides a-z.

这将匹配并排除包含除 之外的其他内容的行a-z

cat file.txt | grep -v '[^[:lower:]]'

If you need to allow symbols too (this example allows !, +, ,):

如果您也需要允许符号(此示例允许!, +, ,):

cat file.txt | grep -v '[^[:lower:]!+,]'