grep 搜索中间有连字符的字符串
我正在寻找文件夹中包含字符串 abc-def
的文件。
我正在使用 grep -l -r abc-def * ,但我不确定这是否是正确的方法(使用此命令时没有找到文件,但也许它只是意味着没有文件包含字符串)。我还尝试了 grep -l -r 'abc-def' *
(找到文件,但是当我手动查找字符串时,它们不在那里,只有字符串的各个部分,即 abc 和定义)。由于该模式不是以连字符开头 -e 在这里不起作用。
grep 搜索中间带有连字符的字符串的正确方法是什么?
I am looking for files that would contain the string abc-def
in a folder.
I am using grep -l -r abc-def *
, but I am not sure if that is the right way (no files were found when this command was used, but perhaps it simply means that no file contains the string). I have also tried grep -l -r 'abc-def' *
(found files, but when I manually looked for the string they were not there, only the individual parts of the string, i.e. abc and def). Since the pattern does not start with a hyphen -e would not work here.
What would be the proper way to grep search for a string with a hyphen in the middle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先尝试 grep -r abc-def 来查看哪些行匹配。 grep -r abc-def * 和 grep -r 'abc-def' * 应该产生相同的结果。
fgrep
(f 代表“固定字符串”)在这里不是必需的。Try
grep -r abc-def
first to see which lines match.grep -r abc-def *
andgrep -r 'abc-def' *
should really yield the same result.fgrep
(f is for 'fixed string`) is not necessary here.这应该有效:
this should work: