我需要一个类似于grep的工具来搜索纯文本字符串。我正在寻找在另一个日志文件的一行中出现的日志文件行的情况。搜索文本可以包含各种正则表达式特殊字符,例如[]().*^$-\
。
是否有一种Unix搜索工具不使用正则表达式,而只是搜索字符串的字面发生?
-F, --fixed-strings PATTERN is a set of newline-separated fixed strings
-e
选项重复提供多个固定字符串即可。像这样:grep -F -e "fixed1" -e "fixed2" -e "fixed3" -e "fixed4"
。不需要换行符 ;) - ADTCfgrep
。 - majkinetorgrep --include=\*.php -FRn -e "$$"
使用单引号可以得到想要的结果:
grep --include=\*.php -FRn -e '$$'
- Piemol$
在 bash
中有特殊含义,优先于 grep
。单引号覆盖了那个特殊含义。但我不是100%确定。 - ADTC这要么是fgrep
,要么是grep -F
,两者都不会使用正则表达式。fgrep
与grep -F
完全相同,但我更喜欢不必担心参数,因为本质上懒惰 :-)
grep -> grep
fgrep -> grep -F (fixed)
egrep -> grep -E (extended)
rgrep -> grep -r (recursive, on platforms that support it).
-F
传递给grep
。你也可以使用awk,它具有查找固定字符串的能力,以及编程能力,例如only。
awk '{for(i=1;i<=NF;i++) if($i == "mystring") {print "do data manipulation here"} }' file
我非常喜欢GNU grep中可用的-P
标志,可以选择性地忽略特殊字符。
它使得grep -P "^some_prefix\Q[literal]\E$"
成为可能。
来自grep手册
-P, --perl-regexp 将-i解释为Perl兼容正则表达式(PCRE)。 当与-z(--null-data)选项结合使用时,此选项是实验性的,并且grep -P可能会警告未实现的功能。
cat list.txt
one:hello:world
two:2:nothello
three:3:kudos
grep --color=always -F"hello
three" list.txt
one:hello:world
three:3:kudos
grep
搜索字面字符串吗?”而是“有没有像grep
这样可以搜索字面字符串的东西?”你知道,平头螺丝刀也可以适用于十字螺丝头;)
。 - ADTC