Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste the result of the tee command

Tags:

bash

This is only hypothetical question - not solving any real problem - only learning bash.

With the tee command is possible split the output to more different streams, like:

command1 | tee >(commandA1 | commandA2 >file1) >(commandB1 | commandB2 >file2) >file0

so graphically is done the next

                  ---commandA1---commandA2--> file1
                 /
command1---tee-------> file0
                 \
                  ---commandB1---commandB2--> file2

Now, with the paste command can e.g.

paste file1 file2 | command3

but again i can redirect to the paste output from a different programs, like:

paste <(ls) <(ls) | command3

The question is: is possible somewhat join the two streams into one, something like

                  ---commandA1---commandA2---
                 /                           \
command1---tee-------> file0                  --- paste---command3
                 \                           /
                  ---commandB1---commandB2---

Ps: mean without intermediate files...

like image 239
novacik Avatar asked Jun 24 '13 23:06

novacik


People also ask

What does the tee command do?

The tee command, used with a pipe, reads standard input, then writes the output of a program to standard output and simultaneously copies it into the specified file or files. Use the tee command to view your output immediately and at the same time, store it for future use.

How do you run a tee command?

Use tee followed by any number of files to write the same output to each of them: [command] | tee [options] [filename1] [filename2]... The ls command shows that tee successfully created files example1. txt and example2.

How do I use the tee command in Windows?

The tee command is normally used to split the output of a program so that it can be both displayed and saved in a file. The command can be used to capture intermediate output before the data is altered by another command or program. The tee command reads standard input, then writes its content to standard output.

What does \t do in Linux?

The escapes \t and \a are widely used for tab and bell (the "a" comes from "audible alarm"). Shell scripts use this convention which came from the C language (see example). For example, they are used in the command-line echo and printf utilities (POSIX).


1 Answers

Here's how to do it with named pipes:

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

Working example:

$ 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
like image 58
Barmar Avatar answered Sep 24 '22 17:09

Barmar