【问题标题】:File names only using Git grep仅使用 Git grep 的文件名
【发布时间】:2018-08-04 20:43:47
【问题描述】:

我只想查看文本中包含特定单词的不同文件。

current_directory$ git grep 'word'

显示文件中具有匹配单词的每一行。

所以我尝试了这个

current_directory$ git grep 'word' -- files-with-matches
current_directory$ git grep 'word' -- name-only

但它没有显示任何输出。

另外,我如何计算所有文件中“单词”的总出现次数?

【问题讨论】:

  • 它是--files-with-matches-- 后面没有空格。
  • @Ryan current_directory$ git grep 'word' --files-with-matches fatal: bad flag '--files-with-matches' used after filename
  • 哦,您还必须将选项放在非选项之前。 git grep --files-with-matches 'word'

标签: git grep


【解决方案1】:

错误信息有帮助:

$ git grep 'foo' --files-with-matches
fatal: option '--files-with-matches' must come before non-option arguments

$ git grep --files-with-matches 'foo'
<list of matching files>

要计算字数,这就是我对GNU grep 的处理方式(我不确定git grep 是否有相关选项):

$ grep --exclude-dir=.git -RowF 'foo' | wc -l
717

来自man grep

-R, --dereference-recursive
递归读取每个目录下的所有文件。跟随所有符号链接,不像 -r。

-o, --only-matching
仅打印匹配行的匹配(非空)部分,每个这样的部分都在单独的输出中 行。

-w, --word-regexp
仅选择那些包含构成整个单词的匹配项的行。测试是匹配的 子字符串必须位于行首,或者前面有一个非单词组成字符。 同样,它必须位于行尾或后跟非单词组成字符。
构成单词的字符是字母、数字和下划线。

-F, --fixed-strings
将 PATTERN 解释为固定字符串列表(而不是正则表达式),由换行符分隔, 任何一个都将被匹配。

--exclude-dir=目录
从递归搜索中排除与模式 DIR 匹配的目录。

【讨论】:

  • (在Ubuntu MATE 20.04(Focal Fossa)上,'grep' 是 GNU grep(版本 3.4)。)
【解决方案2】:
git grep --files-with-matches 'bar'

"--files-with-matches" 需要在您的搜索字符串之前

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多