首页 > 解决方案 > 奇怪的行为 grep -rnw

问题描述

我在 MacOS 中使用 grep (BSD grep) 2.5.1-FreeBSD,我发现了以下行为。

我有两个 *.tex 文件。每一个都包含以下几行

$k$-th bit of
$(i-m)$-th bit of

分别。当我跑

grep --color -rnw . -e '\$-th bit of' --include="*.tex" 

我只得到了第二个文件,即 $(im)$-th 位,而我期望这两行。你能帮我理解这种行为吗?

标签: grep

解决方案


永远不要使用-ror--include或任何其他 grep 选项来查找文件。find当有一个非常好的工具用于查找文件时,GNU 家伙真的把这些选项添加到 grep 中搞砸了,现在他们已经将 grep 变成了查找文件和全局匹配文件中的正则表达式并打印结果( G/RE/P)。

保持简单 -使用g/re/p找到文件,find然后使用:grep

find . -name '*.tex' -exec grep --color -n '\$-th bit of' {} +

正如其他人指出的那样,您的g/re/p问题是-warg 所以我在上面删除了它。


推荐阅读