bash 如何使用grep和regex查找双引号字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16267585/
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 a double quoted string using grep and regex?
提问by Itai Ganot
I'm trying to find all the occurrences of the string "5.1" in all files under a certain directory. I've tried using the following command but it finds none:
我试图在某个目录下的所有文件中找到所有出现的字符串“5.1”。我试过使用以下命令,但没有找到:
#grep -Re[5]+\.+[1] .
What am I doing wrong?
我究竟做错了什么?
回答by linuts
You don't need e regex for that, so fgrep is simpler to construct and executes quicker
你不需要 e regex,所以 fgrep 更容易构建和执行更快
fgrep -R '"5.1"' .
回答by Carl Norum
Your expression doesn't make sense to me. It seems like you're looking for:
你的表达对我来说没有意义。似乎您正在寻找:
grep -R '5\.1' .
Or if you want to include the "characters:
或者,如果您想包含"字符:
grep -R '"5\.1"' .
But I don't see anything related to those in your original post, so it's hard to say.
但是我没有看到与您原始帖子中的内容相关的任何内容,因此很难说。
回答by Jens
Looking for a fixed string recursively would be
递归地寻找一个固定的字符串将是
grep -fR '"5.1"' .
(if you really meant including the double quotes).
(如果你真的想包括双引号)。
Note that the -Roption is not POSIX, so may not be available. In that case, the portable solution is
请注意,该-R选项不是 POSIX,因此可能不可用。在这种情况下,便携式解决方案是
find . -type f | xargs grep -f '"5.1"'

