Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xargs with multiple arguments

I have a source input, input.txt

a.txt b.txt c.txt 

I want to feed these input into a program as the following:

my-program --file=a.txt --file=b.txt --file=c.txt 

So I try to use xargs, but with no luck.

cat input.txt | xargs -i echo "my-program --file"{} 

It gives

my-program --file=a.txt my-program --file=b.txt my-program --file=c.txt 

But I want

my-program --file=a.txt --file=b.txt --file=c.txt 

Any idea?

like image 924
Howard Avatar asked Sep 22 '10 14:09

Howard


People also ask

How do you use xargs with multiple arguments?

Two Types of Commands Using Multiple Arguments Commands can have multiple arguments in two scenarios: All command arguments – COMMAND ARG1 ARG2 ARG3. Option arguments – for example, COMMAND -a ARG1 -b ARG2 -c ARG3.

How do you pass arguments to xargs?

The -c flag to sh only accepts one argument while xargs is splitting the arguments on whitespace - that's why the double quoting works (one level to make it a single word for the shell, one for xargs).

Does xargs run in parallel?

xargs will run the first two commands in parallel, and then whenever one of them terminates, it will start another one, until the entire job is done. The same idea can be generalized to as many processors as you have handy. It also generalizes to other resources besides processors.

How do I use xargs with grep?

Combine xargs with grep Use xargs with the grep command to search for a string in the list of files provided by the find command. The example above searched for all the files with the . txt extension and piped them to xargs , which then executed the grep command on them.


2 Answers

Don't listen to all of them. :) Just look at this example:

echo argument1 argument2 argument3 | xargs -l bash -c 'echo this is first:$0 second:$1 third:$2' 

Output will be:

this is first:argument1 second:argument2 third:argument3 
like image 173
sauliux Avatar answered Oct 19 '22 00:10

sauliux


None of the solutions given so far deals correctly with file names containing space. Some even fail if the file names contain ' or ". If your input files are generated by users, you should be prepared for surprising file names.

GNU Parallel deals nicely with these file names and gives you (at least) 3 different solutions. If your program takes 3 and only 3 arguments then this will work:

(echo a1.txt; echo b1.txt; echo c1.txt;  echo a2.txt; echo b2.txt; echo c2.txt;) | parallel -N 3 my-program --file={1} --file={2} --file={3} 

Or:

(echo a1.txt; echo b1.txt; echo c1.txt;  echo a2.txt; echo b2.txt; echo c2.txt;) | parallel -X -N 3 my-program --file={} 

If, however, your program takes as many arguments as will fit on the command line:

(echo a1.txt; echo b1.txt; echo c1.txt;  echo d1.txt; echo e1.txt; echo f1.txt;) | parallel -X my-program --file={} 

Watch the intro video to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ

like image 36
Ole Tange Avatar answered Oct 19 '22 01:10

Ole Tange