bash 如何 grep 包含下划线的关键字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23379292/
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 can I grep a keyword which contain underscore?
提问by taymedee
I am currently searching some keyword something like
我目前正在搜索一些关键字,例如
find -type f | xargs -grep -i -w 'weblogic_*'
find -type f | xargs -grep -i -w 'weblogic_*'
But it show all the keyword match with weblogic
instead of weblogic_
但它显示所有与weblogic
而不是匹配的关键字weblogic_
回答by mklement0
grep
uses regular expressions, not globs (wildcard expressions).
grep
使用正则表达式,而不是 globs(通配符表达式)。
In regular expressions, *
is a quantifier that relates to the previous character or expression. Thus, _*
is saying: zero or moreinstances of _
, so NO _
will be matched as well.
在正则表达式中,*
是与前一个字符或表达式相关的量词。因此,_*
是说: 的零个或多个实例_
,因此 NO_
也将匹配。
You probably want:
你可能想要:
'weblogic_.*'
which states that any (.
) character may follow the _
zero or more times.
这表明任何 ( .
) 字符都可以跟随_
零次或多次。
Note, however, that ending your regex in _.*
partially contradicts grep
's -w
flag in that it will now only match the beginningof your regex on a word boundary.
但是请注意,正则表达式的结尾_.*
部分与grep
的-w
标志相矛盾,因为它现在只会匹配单词边界上正则表达式的开头。
If you wanted to be more explicit about this, you could use the word-boundary assertion \b
and drop the -w
option:
如果你想更明确地说明这一点,你可以使用 word-boundary 断言\b
并删除-w
选项:
'\bweblogic_'
As you can see, this allows you to omit the .*
, as grep
performs substringmatching by default, and you needn't match the remainder of the line if it is not of interest.
如您所见,这允许您省略.*
, 因为默认情况下grep
执行子字符串匹配,如果您不感兴趣,则不需要匹配该行的其余部分。
Also, there is no need for xargs
: it is simpler and more efficient to use find
's -exec
primary, which has xargs
built in, so to speak:
此外,不需要xargs
: 使用内置的find
's -exec
primary更简单、更高效xargs
,可以这么说:
find . -type f -exec grep -i '\bweblogic_' {} +
{}
represents the list of input filenames and +
specifies that as many input filenames as possible should be passed at once - as with xargs
.
{}
表示输入文件名的列表,并+
指定应一次传递尽可能多的输入文件名 - 与xargs
.
Finally, if your grep
version supports the -R
option, you can make do without find
altogether and simply let grep process all files recursively:
最后,如果您的grep
版本支持该-R
选项,您可以完全不用find
,只需让 grep 递归处理所有文件:
grep -R -i '\bweblogic_' .
回答by R Sahu
When you use the pattern weblogic_*
, it means look for weblogic
followed by zero or more occurrences of _
.
当您使用该模式时weblogic_*
,这意味着查找weblogic
后跟零次或多次出现的_
。
You can change it to use the pattern weblogic_.*
if you want to avoid matching weblogic
that is not followed by a _
.
weblogic_.*
如果您想避免weblogic
后面没有跟的匹配,您可以将其更改为使用模式_
。
find -type f | xargs -grep -i -w 'weblogic_.*'
should work.
应该管用。
回答by Amit Verma
Try without regex
尝试不使用正则表达式
find . -type f | xargs grep -i 'weblogic_'
回答by Taher Khorshidi
simply use this :
简单地使用这个:
grep -i '^weblogic_.*'