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?

1
0

You may use this for split:

echo "This is a sentence" |
  parallel --tee --pipe -q cut -d ' ' -f {} ::: 1,3 2,4

echo "This is a sentence" |
  parallel --tee --pipe "cut -d ' ' -f {} >file{#}" ::: 1,3 2,4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.