Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe two different outputs into a command that takes two inputs

It seems like this should be pretty easy but it's not intuitive to me how to do it. I have two files and I want to diff their first columns (this is an example, I'm sure there are other ways to do this). So I might do cut -d, -f1 file1 > tmp1, cut -d, -f1 file2 > tmp2 and then diff tmp1 tmp2. But I want to do it without using the tmp files.

An example of the sort of thing I'm expecting would be ((cut -d, -f1 file1), (cut -d, -f1 file2)) > diff but this is not real code.

Is there a way to do this?

like image 524
Andrew Latham Avatar asked Feb 26 '14 20:02

Andrew Latham


People also ask

How do you pipe the output of a command to another command?

The | command is called a pipe. It is used to pipe, or transfer, the standard output from the command on its left into the standard input of the command on its right. # First, echo "Hello World" will send Hello World to the standard output.

How can you use pipe in multiple commands?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.

Which character is used to pipe output from one command to another?

The pipe character | is used to connect the output from one command to the input of another. > is used to redirect standard output to a file.


1 Answers

Good news! You can use process substitution in bash:

diff <(cut -d, -f1 file1) <(cut -d, -f1 file2)
like image 152
hek2mgl Avatar answered Oct 08 '22 21:10

hek2mgl