How do I join two named pipes into single input stream in linux

Using the pipes (|) feature in Linux I can forward chain the standard input to one or several output streams.

I can use tee to split the output to separate sub processes.

Is there a command to join two input streams?

How would I go about this? How does diff work?


Solution 1:

Personally, my favorite (requires bash and other things that are standard on most Linux distributions)

The details can depend a lot on what the two things output and how you want to merge them ...

Contents of command1 and command2 after each other in the output:

cat <(command1) <(command2) > outputfile

Or if both commands output alternate versions of the same data that you want to see side-by side (I've used this with snmpwalk; numbers on one side and MIB names on the other):

paste <(command1) <(command2) > outputfile

Or if you want to compare the output of two similar commands (say a find on two different directories)

diff <(command1) <(command2) > outputfile

Or if they're ordered outputs of some sort, merge them:

sort -m <(command1) <(command2) > outputfile

Or run both commands at once (could scramble things a bit, though):

cat <(command1 & command2) > outputfile

The <() operator sets up a named pipe (or /dev/fd) for each command, piping the output of that command into the named pipe (or /dev/fd filehandle reference) and passes the name on the commandline. There's an equivalent with >(). You could do: command0 | tee >(command1) >(command2) >(command3) | command4 to simultaneously send the output of one command to 4 other commands, for instance.

Solution 2:

You can append two steams to another with cat, as gorilla shows.

You can also create a FIFO, direct the output of the commands to that, then read from the FIFO with whatever other program:

mkfifo ~/my_fifo
command1 > ~/my_fifo &
command2 > ~/my_fifo &
command3 < ~/my_fifo

Particularly useful for programs that will only write or read a file, or mixing programs that only output stdout/file with one that supports only the other.

Solution 3:

(tail -f /tmp/p1 & tail -f /tmp/p2 ) | cat > /tmp/output

/tmp/p1 and /tmp/p2 are your input pipes, while /tmp/output is the output.

Solution 4:

I have created special program for this: fdlinecombine

It reads multiple pipes (usually program outputs) and writes them to stdout linewise (you can also override the separator)

Solution 5:

A really cool command I have used for this is tpipe, you might need to compile because it not that common. Its really great for doing exactly what your talking about, and it's so clean I usually install it. The man page is located here http://linux.die.net/man/1/tpipe . The currently listed download is at this archive http://www.eurogaran.com/downloads/tpipe/ .

It's used like this,

## Reinject sub-pipeline stdout into standard output:
$ pipeline1 | tpipe "pipeline2" | pipeline3