2011-03-18 69 views
0

我知道當使用findxargs時,建議使用-print0-0參數以使文件名的空格正確工作。瞭解空格和xargs

現在我有一個名爲patterns具有以下內容下列文件

a a a 
b b b 

和我有以下內容命名text文件

a 
b b b 

當我運行下面的命令,

cat patterns| xargs -ipattern grep pattern text 

我得到

b b b 

這意味着xargs的認識,尋找a a ab b b代替aaabbb

我的問題是爲什麼在上面的例子中沒有任何問題?我認爲它會尋找a,a,a,b,b,b並返回text的兩行。

我錯過了什麼?

回答

1

-i-I將它從使用(可能引用的)空格分隔的字符串更改爲行。引用man 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. 

    --replace[=replace-str] 
    -i[replace-str] 
      This option is a synonym for -Ireplace-str if replace-str is specified, and for 
      -I{} otherwise. This option is deprecated; use -I instead. 

當你在默認的文件名工作(即不-I)模式下,你需要保護空格,製表符,以及(這並不是說像這樣的文件名是永遠的好主意)換行;因此-0

+0

啊,這一切都歸結爲閱讀手冊更仔細。我的錯。謝謝。 – Russell 2011-03-18 19:48:44