2011-03-04 71 views

回答

7

除非它在您的問題發佈一個錯字不應該有連字符sh前:

你沒有得到的原因輸出文件名是grep正與一個文件作爲參數運行。要強制文件名輸出使用-H

find . -name "*.xml" | xargs -I {} sh -c "grep -H FOO {}" 

此外,-ixargs各地4.2.9版本被棄用。您應該使用-I {}

0

你可以使用

find . -name "*.xml" | xargs -I{} grep FOO {} 

,並根據您的需要,你可以使用-H-ngrep命令。

+1

不提供答案的規定的問題。 – andr 2013-03-14 02:42:20

4

正如之前的回答所說的,不同之處在於每個文件都會調用grep,如果在命令行中只指定了一個文件,grep就不報告文件名,除非-H(--with-filename)國旗給出。

爲什麼每個文件都會調用grep?這是因爲(喜歡或不喜歡)對xargs使用-I(或-i)標誌會強制命令對每個參數運行一次,如使用標誌「-L 1」到xargs。

從手冊頁:

-I replace-str 
    Replace occurrences of replace-str in the initial-arguments with names read from 
    standard input. Also, unquoted blanks do not terminate input items; instead the 
    separator is the newline character. Implies -x and -L 1.