【问题标题】:Understanding white spaces and xargs了解空格和 xargs
【发布时间】:2011-07-18 10:27:07
【问题描述】:

我了解到,在将findxargs 一起使用时,建议对带有空格的文件名使用-print0-0 参数才能正常工作。

现在我有一个名为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

我的问题是为什么上面的例子没有任何问题?我以为它会寻找aaabbb 并在text 中返回这两行。

我错过了什么?

【问题讨论】:

    标签: grep xargs cat


    【解决方案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

    【讨论】:

    • 啊,这一切都归结为更仔细地阅读手册。我的错。谢谢。
    猜你喜欢
    • 2010-12-26
    • 2010-11-10
    • 1970-01-01
    • 2015-12-09
    • 2015-08-17
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    相关资源
    最近更新 更多