grep --include 命令在 OSX Zsh 中不起作用

2024-04-22

我正在关注最佳答案如何在 Linux 上查找包含特定文本的所有文件? https://stackoverflow.com/questions/16956810/finding-all-files-containing-a-text-string-in-linux/16957078#16957078在我的项目中搜索字符串。

这是我的命令grep --include=*.rb -rnw . -e "pattern"

Zsh 告诉我zsh: no matches found: --include=*.rb

grep 好像不支持--include option.

当我打字时grep --help,它返回

usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
    [-e pattern] [-f file] [--binary-files=value] [--color=when]
    [--context[=num]] [--directories=action] [--label] [--line-buffered]
    [--null] [pattern] [file ...]

no --include here.

我的 grep 版本太旧了吗?还是我的命令有问题?


FreeBSD/macOSgrep does支持--include选项(参见man grep;不幸的是命令行 help (grep -h) 没有列出此选项),但是你的问题是选项参数,*.rb, is unquoted.

结果就是你的外壳, zsh, that 尝试扩展路径名--include=*.rb预先,并且失败,因为当前目录不包含名称与 glob 模式匹配的文件*.rb.
grep甚至永远无法执行。

既然你的意图是通过*.rb 未修改为grep, 你必须quote it:

grep --include='*.rb' -rnw . -e "pattern"     

包括multiple globs:

  • Pass an --include option for each; e.g.:

    grep --include='*.rb' --include=='*.h*' -rnw . -e "pattern"     
    
  • 或者,在支持的外壳中大括号扩展- 尤其bash, ksh, and zsh- 你可以让你的shell为您创建多个选项,如下 -请注意选择性引用 (see 这个答案 https://stackoverflow.com/a/44121121/45375详细解释):

    grep '--include=*.'{rb,'h*'} -rnw . -e "pattern"     
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

grep --include 命令在 OSX Zsh 中不起作用 的相关文章

随机推荐