gnu并行:结合使用--pipe和args

fin*_*mer 4 bash gnu-parallel

使用--pipe -N<int>I 可以发送给定数量的行作为由 启动的作业的输入parallel:::但是我怎样才能运行多个作业,并在每个块上给出不同的参数呢?

让我们看一下这个小输入文件:

A   B   C
D   E   F
G   H   I
J   K   L
Run Code Online (Sandbox Code Playgroud)

此外,我们定义将每两行通过管道传输到一个parallel作业。cut -f<int>在它们上,应该使用作为并行输入参数给出的列号来执行命令,例如::: {1..3}

因此对于给定的示例,输出将如下所示

A
D
B
E
C
F
G
J
H
K
I
L
Run Code Online (Sandbox Code Playgroud)

我尝试过这个命令:

cat input.txt|parallel --pipe -N2 'cut -f{1}' ::: {1..3}
Run Code Online (Sandbox Code Playgroud)

但输出是这样的:

A
D
I
L
Run Code Online (Sandbox Code Playgroud)

我缺少什么?

鳍游泳者

Ole*_*nge 5

这:

cat input.txt|parallel --pipe -N2 'cut -f{1}' ::: {1..3}
Run Code Online (Sandbox Code Playgroud)

从每个输入源读取 2 条记录。如果你这样做,那就更清楚了:

$ cat input.txt|parallel --pipe -v -N2 'cut -f{}' ::: {1..3}
cut -f1  -f2
cut: only one type of list may be specified
Try 'cut --help' for more information.
cut -f3
I
L
Run Code Online (Sandbox Code Playgroud)

GNU Parallel 将每个参数与一个块配对。您正在寻找的更像是--tee每个块都发送到每个命令的地方。--tee但是,不会将输入切成块,而是将所有输入发送到命令。所以也许我们可以将两者结合起来:

doit() { parallel --pipe -N2 -v cut -f$@; }
export -f doit
cat input.txt|parallel --pipe --tee -v doit {} ::: {1..3}
Run Code Online (Sandbox Code Playgroud)

或者你可以翻转顺序(这可能效率较低):

doit() { parallel -v --pipe --tee cut -f{} ::: {1..3}; }
export -f doit
cat input.txt|parallel --pipe -N2 -v doit
Run Code Online (Sandbox Code Playgroud)

-v当您对正在运行的内容感到满意时删除。

--tee非常高效( 1-2 GBytes/s --pipe, 2-3 GBytes/s --pipepart),但它有一个缺点,它并行启动所有作业:因此,如果您有 10000 个值,而不是 {1..3},那么它将启动10000个进程。