【问题标题】:xargs with grep passes an empty string带有 grep 的 xargs 传递一个空字符串
【发布时间】:2016-06-25 04:58:54
【问题描述】:

当我使用 xargs 将文件名传递给 grep 时,grep: : No such file or directory 来自哪里?

⋊> ~/.v/bundle find . -name config | grep ".git" | xargs grep "url" {}                                                                                11:07:46
grep: : No such file or directory
./Dockerfile.vim/.git/config:   url = https://github.com/ekalinin/Dockerfile.vim.git
./nerdcommenter/.git/config:    url = https://github.com/scrooloose/nerdcommenter.git

虽然我这样做时没有空行或类似的东西

⋊> ~/.v/bundle find . -name config | grep ".git"                                                                                                      11:08:30
./Dockerfile.vim/.git/config
./nerdcommenter/.git/config

又该如何避免呢?

我知道我可以用awk 'NR>1' 之类的内容跳过第一行,但想了解问题的原因。

谢谢。

附:我在 Mac 上。

【问题讨论】:

    标签: bash grep xargs


    【解决方案1】:

    很难说您的网站出了什么问题。我试图重现错误,也有.vim/bundle,但我无法重现它。

    无论如何,我建议将命令简化为:

    find . -wholename '*/.git/config' -exec grep -H url {} +
    

    xargs 不再需要,因为find 支持+ 语法。对于find 的GNU、BSD (MacOS) 和busybox 版本来说至少是这样。

    【讨论】:

    • 感谢您的建议。我喜欢这个主意,但不幸的是它对我不起作用。我在mac上。 ⋊> ~/.v/bundle 查找 . -wholename '*/.git/config' -exec grep -H url {} + 20:31:23 find: -exec: no terminating ";"或 "+" ⋊> ~/.v/bundle gfind 。 -wholename '*/.git/config' -exec grep -H url {} + 20:31:34 gfind: `-exec' 缺少参数
    • 奇怪!然后使用{} \; 而不是{} +
    • 不。它为我返回grep: : No such file or directory
    【解决方案2】:

    无需添加{}(与find -exec不同)

    ~/.v/bundle find . -name config | grep ".git" | xargs grep "url" 
    

    应该可以。

    甚至更好(仅在文件或目录名称包含空格或不寻常字符的情况下):

    ~/.v/bundle find . -name config -print0 | grep -z ".git" | xargs --null grep "url"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多