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
How to find lines that contain ONLY lowercase using grep
提问by Roosh
I am new to bash and am learning to use grep.
我是 bash 新手,正在学习使用 grep。
grep ^[a-z] file.txt
will show all the lines that begin with lowercasegrep [a-z] file.txt
all 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 egrep
will 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:]!+,]'