Linux Grep 不是正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9416390/
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
Grep not as a regular expression
提问by dotancohen
I need to search for a PHP variable $someVar
. However, Grep thinks that I am trying to run a regex and is complaining:
我需要搜索一个 PHP 变量$someVar
。但是,Grep 认为我正在尝试运行正则表达式并抱怨:
$ grep -ir "Something Here" * | grep $someVar
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
$ grep -ir "Something Here" * | grep "$someVar"
<<Here it returns all rows with "someVar", not only those with "$someVar">>
I don't see an option for telling grep notto interpret the string as a regex, but to include the $
as just another string character.
我没有看到告诉 grep不要将字符串解释为正则表达式的选项,而是将 包含$
为另一个字符串字符。
采纳答案by Mr Lister
Escape the $
by putting a \
in front of it.
$
通过\
在它前面放一个来逃避它。
回答by Hasturkun
Use fgrep
(deprecated), grep -F
or grep --fixed-strings
, to make it treat the pattern as a list of fixed strings, instead of a regex.
使用fgrep
(deprecated), grep -F
orgrep --fixed-strings
使其将模式视为固定字符串列表,而不是正则表达式。
For reference, the documentation mentions (excerpts):
作为参考,文档提到(摘录):
-F
--fixed-strings
Interpret the pattern as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched. (-F is specified by POSIX.)
fgrep
is the same asgrep -F
. Direct invocation as fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.
-F
--fixed-strings
将模式解释为由换行符分隔的固定字符串列表(而不是正则表达式),其中任何一个都将被匹配。(-F 由 POSIX 指定。)
fgrep
与 相同grep -F
。作为 fgrep 的直接调用已被弃用,但提供它是为了允许依赖它们的历史应用程序未经修改地运行。
For the complete reference, check: https://www.gnu.org/savannah-checkouts/gnu/grep/manual/grep.html
如需完整参考,请查看:https: //www.gnu.org/savannah-checkouts/gnu/grep/manual/grep.html
回答by rkhayrov
grep -F
is a standard way to tell grep
to interpret argument as a fixed string, not a pattern.
grep -F
是grep
将参数解释为固定字符串而不是模式的标准方法。
回答by huelbois
You have to tell grep you use a fixed-string, instead of a pattern, using '-F' :
您必须使用 '-F' 告诉 grep 您使用的是固定字符串,而不是模式:
grep -ir "Something Here" * | grep -F $somevar
回答by Christophe Drevet-Droguet
In this question, the main issue is not about grep
interpreting $
as a regex. It's about the shell substituting $someVar
with the value of the environment variable someVar
, likely the empty string.
在这个问题中,主要问题不在于grep
解释$
为正则表达式。这是关于 shell 替换$someVar
环境变量的值someVar
,可能是空字符串。
So in the first example, it's like calling grep
without any argument, and that's why it gives you a usage
output. The second example should not return all rows containing someVar
but all lines, because the empty string is in all lines.
所以在第一个例子中,这就像grep
不带任何参数的调用,这就是为什么它会给你一个usage
输出。第二个示例不应返回包含someVar
但所有行的所有行,因为空字符串在所有行中。
To tell the shell to not substitute, you have to use '$someVar'
or \$someVar
. Then you'll have to deal with the grep interpretation of the $
character, hence the grep -F
option given in many other answers.
要告诉外壳不要替换,您必须使用'$someVar'
或\$someVar
。然后你将不得不处理$
字符的 grep 解释,因此grep -F
在许多其他答案中给出了选项。
So one valid answer would be:
所以一个有效的答案是:
grep -ir "Something Here" * | grep '$someVar'
回答by Alo?ké Go
+1 for the -F
option, it shall be the accepted answer.
Also, I had a "strange" behaviour while searching for the -I..
pattern in my files, as the -I
was considered as an option of grep
; to avoid such kind of errors, we can explicitly specify the end of the arguments of the command using --
.
+1为-F
选项,它应该是被接受的答案。此外,-I..
在我的文件中搜索模式时,我有一个“奇怪”的行为,因为它-I
被认为是grep
; 为避免此类错误,我们可以使用 明确指定命令参数的结尾--
。
Example:
例子:
grep -HnrF -- <pattern> <files>
Hope that'll help someone.
希望这会帮助某人。