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

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

How can I grep a keyword which contain underscore?

bash

提问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 weblogicinstead of weblogic_

但它显示所有与weblogic而不是匹配的关键字weblogic_

回答by mklement0

grepuses 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 -wflag 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 \band drop the -woption:

如果你想更明确地说明这一点,你可以使用 word-boundary 断言\b并删除-w选项:

'\bweblogic_'

As you can see, this allows you to omit the .*, as grepperforms 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 -execprimary, which has xargsbuilt in, so to speak:

此外,不需要xargs: 使用内置的find's -execprimary更简单、更高效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 grepversion supports the -Roption, you can make do without findaltogether 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 weblogicfollowed by zero or more occurrences of _.

当您使用该模式时weblogic_*,这意味着查找weblogic后跟零次或多次出现的_

You can change it to use the pattern weblogic_.*if you want to avoid matching weblogicthat 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_.*'