了解空格和 xargs

发布于 2024-10-24 03:53:48 字数 764 浏览 7 评论 0原文

我了解到,当将 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知道寻找 aa abb b 而不是 aaa >、bbb

我的问题是为什么上面的例子没有任何问题?我认为它会寻找 aaabb , b 并返回 text 中的两行。

我缺少什么?

I've learned that when using find with xargs, it's advisable to use the -print0 and -0 arguments for file names with spaces to work correctly.

Now I have the following file named patterns with the following content

a a a
b b b

and I have the file named text with the following content

a
b b b

When I run the following command,

cat patterns| xargs -ipattern grep pattern text

I get

b b b

which means that xargs knew to look for a a a and b b b instead of a, a, a, b, b, b.

My question is why isn't there any problems in the example above? I thought it would look for a, a, a, b, b, b and return both lines in text.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2024-10-31 03:53:48

-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

-i and -I change it from using (possibly quoted) whitespace-separated strings to lines. Quoting 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.

When you're working with filenames in default (i.e. not -I) mode, you need to protect spaces, tabs, and (not that filenames like this are ever a good idea) newlines; hence -0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文