拆分不同命令的输入并组合结果

use*_*129 8 bash pipe shell-script text-processing process-substitution

我知道如何组合不同命令的结果

paste -t',' <(commanda) <(commandb)
Run Code Online (Sandbox Code Playgroud)

我知道将相同的输入管道传输到不同的命令

cat myfile | tee >(commanda) >(commandb)
Run Code Online (Sandbox Code Playgroud)

现在如何组合这些命令?这样我才能做到

cat myfile | tee >(commanda) >(commandb) | paste -t',' resulta resultb
Run Code Online (Sandbox Code Playgroud)

说我有一个文件

我的文件:

1
2
3
4
Run Code Online (Sandbox Code Playgroud)

我想新建一个文件

1 4 2
2 3 4
3 2 6
4 1 8
Run Code Online (Sandbox Code Playgroud)

我用了

cat myfile | tee >(tac) >(awk '{print $1*2}') | paste
Run Code Online (Sandbox Code Playgroud)

会给我垂直的结果,我真的想以水平顺序粘贴它们。

gle*_*man 8

当您进行多个进程替换时,不能保证以任何特定顺序获得输出,因此您最好坚持使用

paste -t',' <(commanda < file) <(commandb < file)
Run Code Online (Sandbox Code Playgroud)

假设cat myfile代表一些昂贵的管道,我认为您必须将输出存储在文件或变量中:

output=$( some expensive pipeline )
paste -t',' <(commanda <<< "$output") <(commandb <<< "$output")
Run Code Online (Sandbox Code Playgroud)

使用您的示例:

output=$( seq 4 )
paste -d' ' <(cat <<<"$output") <(tac <<<"$output") <(awk '$1*=2' <<<"$output")
Run Code Online (Sandbox Code Playgroud)
1 4 2
2 3 4
3 2 6
4 1 8
Run Code Online (Sandbox Code Playgroud)

另一个想法:FIFO 和单个管道

mkfifo resulta resultb
seq 4 | tee  >(tac > resulta) >(awk '$1*=2' > resultb) | paste -d ' ' - resulta resultb
rm resulta resultb
Run Code Online (Sandbox Code Playgroud)
1 4 2
2 3 4
3 2 6
4 1 8
Run Code Online (Sandbox Code Playgroud)