小编典典

具有多个参数的xargs

linux

我有一个源输入 input.txt

a.txt
b.txt
c.txt

我想将这些输入馈入程序,如下所示:

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

所以我尝试使用 xargs ,但是没有运气。

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

它给

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

但我想要

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

任何的想法?


阅读 535

收藏
2020-06-03

共1个答案

小编典典

到目前为止给出的解决方案都无法正确处理包含空格的文件名。如果文件名包含“或”,有些甚至会失败。如果输入文件是由用户生成的,则应该准备好使用令人惊讶的文件名。

GNU
Parallel
很好地处理了这些文件名,并为您(至少)提供了3种不同的解决方案。如果您的程序只接受3个参数,则只有3个参数,那么它将起作用:

(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}

要么:

(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={}

但是,如果您的程序接受的参数与命令行上的参数一样多:

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

观看介绍性视频以了解更多信息:http :
//www.youtube.com/watch?v=OpaiGYxkSuQ

2020-06-03