2011-06-06 61 views
30

与GNU命令行参数使用GNU parallelhttp://www.gnu.org/software/parallel/分裂平行

我有一个程序,采用两个参数,例如

$ ./prog file1 file2 
$ ./prog file2 file3 
... 
$ ./prog file23456 file23457 

我正在使用生成文件名对的脚本,但是由于脚本的结果是单个字符串 - 不是一对,所以会产生问题。像:

$ ./prog "file1 file2" 

GNU parallel似乎有招数起袖子的转换,我不知道是否有一个周边分离分裂的文本:

$ generate_file_pairs | parallel ./prog ? 
    # where ? is text under consideration, like "file1 file2" 

最简单的解决方法是手动的分成ARGS中编,但我想知道是否有可能在GNU parallel

回答

54

您可能正在寻找--colsep

generate_file_pairs | parallel --colsep ' ' ./prog {1} {2} 

阅读man parallel了解更多。并观看介绍视频,如果你还没有这样做http://www.youtube.com/watch?v=OpaiGYxkSuQ

+0

这就是它,谢谢! – drhodes 2011-06-07 06:27:52

+0

当我读到最初的问题时,它看起来像“generate_file_pairs”会用引号输出。 '--colsep'不会删除引号,对吗?假设引号包围文本,是否有办法平行修剪它们?例如,以下不起作用:'echo'“file1 file2”'| parallel --colsep''./prog {1} {2}' – 2014-01-09 22:52:20

+0

从20140722版本开始:echo''file1 file2''| parallel --colsep''echo'{= 1 s/^“// =} - {= 2 s /”$ // =}' – 2015-03-26 16:00:08

1

您正在寻找并联的-n选项。这是你在找什么:从GNU Parallel Doc

./generate_file_pairs | parallel -n 2 ./prog {} 

摘录:

-n max-args 
    Use at most max-args arguments per command line. Fewer than max-args 
    arguments will be used if the size (see the -s option) is exceeded, 
    unless the -x option is given, in which case GNU parallel will exit. 
+0

好了,但它仍然有-n选项。我会相应地编辑。 – ssapkota 2011-06-06 21:19:28

+0

这不会做分裂。例如:'echo hi there |并行-n 2 echo {2} x {1}'=> x hi there(在这种情况下没有{2}。)使用--colsep:'echo hi there | parallel -n 2 --colsep''echo {2} x {1}'==> there x hi – 2014-08-25 15:13:38

0

在并行手册,它是说:

如果没有给出命令,执行输入的行... GNU平行可以经常被用作xargs或cat |的替代品庆典。

因此,采取的尝试:

generate command | parallel 

试着去了解这个输出:

for i in {1..5};do echo "echo $i";done | parallel