粘贴tee命令的结果

nov*_*cik 9 bash

这只是一个假设的问题 - 没有解决任何真正的问题 - 只是学习bash.

使用该tee命令可以将输出拆分为更多不同的流,例如:

command1 | tee >(commandA1 | commandA2 >file1) >(commandB1 | commandB2 >file2) >file0
Run Code Online (Sandbox Code Playgroud)

以图形方式完成下一步

                  ---commandA1---commandA2--> file1
                 /
command1---tee-------> file0
                 \
                  ---commandB1---commandB2--> file2
Run Code Online (Sandbox Code Playgroud)

现在,用paste命令就可以了

paste file1 file2 | command3
Run Code Online (Sandbox Code Playgroud)

但我又可以重定向到不同程序的粘贴输出,例如:

paste <(ls) <(ls) | command3
Run Code Online (Sandbox Code Playgroud)

问题是:有可能将两个流合并为一个,类似于

                  ---commandA1---commandA2---
                 /                           \
command1---tee-------> file0                  --- paste---command3
                 \                           /
                  ---commandB1---commandB2---
Run Code Online (Sandbox Code Playgroud)

Ps:没有中间文件的意思......

Bar*_*mar 4

以下是使用命名管道执行此操作的方法:

trap "rm -f /tmp/file1 /tmp/file2; exit 1" 0 1 2 3 13 15
mkfifo /tmp/file1
mkfifo /tmp/file2
command1 | tee >(commandA1 | commandA2 >/tmp/file1) >(commandB1 | commandB2 >/tmp/file2) >file0
paste /tmp/file1 /tmp/file2 | command3
rm -f /tmp/file1 /tmp/file2
trap 0
Run Code Online (Sandbox Code Playgroud)

工作示例:

$ cd -- "$(mktemp -d)" 
$ trap "rm -f pipe1 pipe2; exit 1" 0 1 2 3 13 15
$ mkfifo pipe1 pipe2
$ printf '%s\n' 'line 1' 'line 2' 'line 3' 'line 4' | tee \
>(sed 's/line /l/' | head -n 2 > pipe1) \
>(sed 's/line /Line #/' | tail -n 2 > pipe2) \
> original.txt
$ paste pipe1 pipe2 | sed 's/\t/ --- /'
l1 --- Line #3
l2 --- Line #4
$ rm pipe1 pipe2
$ trap 0
Run Code Online (Sandbox Code Playgroud)