linuxbashshellcommand-line-argumentsxargs

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?


Solution

  • 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