سؤال

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?

هل كانت مفيدة؟

المحلول

-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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top