【问题标题】:How is grep with --include matching this file (when I don't want it to)grep 和 --include 如何匹配这个文件(当我不想要它时)
【发布时间】:2013-09-23 13:34:02
【问题描述】:

好的,我不确定我是否理解为什么此 grep 行匹配的文件未包含在 --include 选项中。我尝试了很多方法来逃避,但似乎无法让它做我想做的事。

目录列表

ryan@ubuntu:~/dir$ ls -lR
.:
total 20
-rw-rw-r-- 1 ryan ryan   19 Sep 23 00:40 bar
-rw-rw-r-- 1 ryan ryan   19 Sep 23 06:18 bar.shit
drwxrwxr-x 2 ryan ryan 4096 Sep 23 04:51 boo
-rw-rw-r-- 1 ryan ryan    4 Sep 23 05:25 foo.txt
-rwxrwxr-x 1 ryan ryan 1597 Sep 23 06:18 rfind
lrwxrwxrwx 1 ryan ryan   13 Sep 23 00:30 ruby -> /usr/bin/ruby

./boo:
total 4
-rw-rw-r-- 1 ryan ryan  0 Sep 23 04:51 bar.foo.txt
-rw-rw-r-- 1 ryan ryan 86 Sep 23 00:44 foo
-rw-rw-r-- 1 ryan ryan  0 Sep 23 04:51 foo.txt

当我期望它只匹配我通过 --include 选择的文件扩展名时,运行以下 grep 行匹配文件“bar”

ryan@ubuntu:~/ass2$ grep --include=*.{rb,erb,js,css,html,yml,txt} -inHR foo ./*
./bar:1:inside here is foo
./bar.shit:1:inside here is foo

我要么没有正确地转义某些东西,要么错过了没有扩展名的文件如何变得有效。

谢谢

【问题讨论】:

    标签: regex bash grep


    【解决方案1】:

    这是因为命令末尾的 glob ./* 导致包含当前目录中的所有文件,而不考虑 --include 指令。如果您将命令更改为

    grep --include=*.{rb,erb,js,css,html,yml,txt} -inHR foo .
    #                                                       ^- Instead of ./*
    

    那么你应该得到你期望的输出。

    【讨论】:

      【解决方案2】:

      因为它是开放的,所以它通过 shell 的全局扩展来扩展自身。正确的做法是:

      grep --include='*.rb' --include='*.erb' --include='*.js' --include='*.css' --include='*.html' --include='*.yml' --include='*.txt' -inHR foo ./*
      

      【讨论】:

        猜你喜欢
        • 2012-11-23
        • 2016-08-26
        • 2013-03-25
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-12
        • 1970-01-01
        相关资源
        最近更新 更多