I need to run an executable a large number of times, each time with two command line arguments. I've used to use
xargs for this purpose, but lately I've been made aware of the existence of GNU
parallel, which in principle seems like a better tool (more features, more up-to-date, more extensive documentation, etc.).
Also, a strong selling point for me was the claim that it can be used as "a drop in replacement for
xargs" (https://www.gnu.org/software/parallel/history.html). However, I'm having a bit of trouble with that last point.
Say that I have a text file,
args.txt, with several lines, where each line contains two numbers separated by a space, for example:
1 2 7 9 11 13
I want to run my program,
run, once for each line (i.e., once for each pair of arguments). With
xargs I would do
cat args.txt | xargs -n2 run
-n2 indicates that
xargs should pass 2 arguments to
run at each invocation.
xargs then interprets each number as one argument, so each line of
args.txt is interpreted as two arguments.
However, when I try using
parallel as a drop-in replacement for
xargs in the case above, I get different behaviour.
To illustrate, I will use the following small python script in place of my program run:
import sys print([x for x in sys.argv[1:]])
Now, with xargs I get:
> cat args.txt | xargs -n2 python printer.py ['1', '2'] ['7', '9'] ['11', '13']
parallel I get
> cat args.txt | parallel -n2 python printer.py ['1 2', '7 9'] ['11 13']
xargs calls the python script with the individual (space-separated) numbers as arguments,
parallel interprets instead each line as a single argument, meaning that for example at the first call, the first argument is
"1 2" instead of just
I'm a little bit confused by this, as I had expected
parallel to work as a drop-in replacement for
xargs, but apparently it's a bit more subtle than that. I suppose my question is how I should use
parallel to achieve the same thing that I'm doing with
xargs, but I'm also just curious about why there is a difference in behaviour here, and if it's intentional.