具有多个参数的xargs [英] xargs with multiple arguments

查看:78
本文介绍了具有多个参数的xargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个源输入, 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

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

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

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

有什么主意吗?

推荐答案

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

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 很好地处理了这些文件名,并为您(至少)提供了3种不同的文件名解决方案.如果您的程序仅接受3个参数,则只有3个参数可以使用:

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}

或者:

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

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

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

这篇关于具有多个参数的xargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆