bash 使用 GNU 并行拆分命令行参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6255286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:07:40  来源:igfitidea点击:

Splitting command line args with GNU parallel

bashfile-processinggnu-parallel

提问by drhodes

Using GNU parallel: http://www.gnu.org/software/parallel/

使用GNU parallelhttp: //www.gnu.org/software/parallel/

I have a program that takes two arguments, e.g.

我有一个需要两个参数的程序,例如

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

I'm using a script that generates the file name pairs, however this poses a problem because the result of the script is a single string - not a pair. like:

我正在使用生成文件名对的脚本,但是这会带来问题,因为脚本的结果是单个字符串 - 而不是一对。喜欢:

$ ./prog "file1 file2"

GNU parallelseems to have a slew of tricks up its sleeves, I wonder if there's one for splitting text around separators:

GNU parallel似乎有很多技巧,我想知道是否有一个用于在分隔符周围拆分文本的技巧:

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

The easy work around is to split the args manually in prog, but I'd like to know if it's possible in GNU parallel.

简单的解决方法是在 prog 中手动拆分 args,但我想知道在GNU parallel.

回答by Ole Tange

You are probably looking for --colsep.

您可能正在寻找--colsep.

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

Read man parallelfor more. And watch the intro video if you have not already done so http://www.youtube.com/watch?v=OpaiGYxkSuQ

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

回答by ssapkota

You are looking for -noption of parallel. This is what you are looking for:

您正在寻找-n并行选项。这就是你要找的:

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

Excerpt from GNU Parallel Doc:

摘自GNU Parallel Doc

-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.

回答by Liang

In Parallel's manual, it is said:

在Parallel的手册中,它说:

If no command is given, the line of input is executed ... GNU parallel can often be used as a substitute for xargs or cat | bash.

如果没有给出命令,则执行输入行... GNU parallel 通常可以用来代替 xargs 或 cat | 猛击。

So take a try of:

所以试试看:

generate command | parallel

Try to understand the output of this:

试着理解这个输出:

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