我需要搜索一个 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">>
我没有看到告诉 grep not 将字符串解释为正则表达式的选项,而是将 $
包含为另一个字符串字符。
最佳答案
使用 fgrep
(已弃用)、grep -F
或 grep --fixed-strings
,使其将模式视为列表固定字符串,而不是正则表达式。
作为引用,文档提到(摘录):
-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.
如需完整引用,请查看: https://www.gnu.org/savannah-checkouts/gnu/grep/manual/grep.html
https://stackoverflow.com/questions/9416390/