How to split and join parallel pipes?

1

I'd like to pipe the output of 1 command into multiple pipes and pipe outputs from multiple commands into 1 single command.

An example of split:

# Single output to multiple pipes
echo "This is a sentence" | split | cut -d ' ' -f 1,3 > file1.txt
                                  | cut -d ' ' -f 2,4 > file2.txt

In the above example of split, the output "This is a sentence" is split piped into 2 cuts and 2 different files.

An example of join:

Hello(){
   echo "Hello $1 and $2"
}

echo "Alice" | 
echo "Bob"   | join | Hello
# output: "Hello Alice and Bob"

In the above example of join takes the outputs from 2 different pipes and give them to the function Hello as 2 piped inputs.

An example of split and join used together:

# split the input into multiple pipes
echo "Alice Bob Charlie Dave" | split

# 2 separate split pipes are processed and then joined into a single pipe
split | cut -d ' ' -f 1,3 | join
split | cut -d ' ' -f 2,4 | join

Hello(){
   echo "$1 loves $2"
}

join | Hello

# Output:
# Alice Charlie loves Bob Dave

The above example implements both split and join to have multiple pipes processed in parallel. split is "single input, multiple outputs", join is "multiple inputs, single output".

What's the best way to implement the demonstrated split and join in a shell/bash script?

John Zhau

Posted 2020-01-19T02:53:44.770

Reputation: 137

Some general ideas: here.

– Kamil Maciorowski – 2020-01-19T20:59:43.063

No answers