2013-01-08 46 views
4

我想在app目錄內搜索字符串foo,但不包括文件名中包含migrations的任何文件。我預計今年grep命令工作grep with --include和--exclude

grep -Ir --include "*.py" --exclude "*migrations*" foo app/ 

上面的命令似乎忽視了--exclude過濾器。作爲替代方案,我可以做

grep -Ir --include "*.py" foo app/ | grep -v migrations 

這工作,但這個失去的foo突出的成績。我還可以將find加入混合並保持突出顯示。

find app/ -name "*.py" -print0 | xargs -0 grep --exclude "*migrations*" foo 

我只是想知道如果我錯過了一些關於命令行參數組合grep或如果他們根本不工作在一起。

+0

在文件名中。 –

回答

0

man grep說:

 --include=GLOB 
      Search only files whose base name matches GLOB (using wildcard matching as described under 
      --exclude). 

因爲它說, 「唯一」 在那裏,我猜你--include statment是壓倒你--exclude聲明。

+0

這就是我在我的問題中所陳述的。我只是好奇,是否有辦法將這兩種選擇結合在一起。 –

2

我一直在尋找一個.py文件的術語,但不想遷移的文件進行掃描,我發現那又怎麼樣(grep的爲2.10)是下面(我希望這有助於):

grep -nR --include="*.py" --exclude-dir=migrations whatever_you_are_looking_for .