Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare the output of two unix commands to find the difference?

Tags:

unix

I prefer not to create new files. I want to accomplish something similar to:

cmd1 > a
cmd2 > b
cat a b b | sort | uniq -u

but without using files a and b.

like image 941
jonderry Avatar asked Nov 06 '10 22:11

jonderry


People also ask

What command can you use to compare two different files?

If you want to compare two files and decipher the difference, a command called “diff” is used. This guide is focused on providing you the usage of the “diff” command with various options to get the difference between two files. So, how does the “diff” command actually function?

How do we compare 2 files using Unix command?

cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not.


1 Answers

Unix utilities are generally file oriented, so nothing quite does what you want.

However, zsh can autocreate temporary files with the following syntax:

diff =(cmd1) =(cmd2)

It can also create temporary named pipes (or use the special files /dev/fdn to reference anonymous pipes) with

diff <(cmd1) <(cmd2)

However, many diffs call lseek() on their input, so won't work with named pipes.

(diff is in general a more useful command for comparing very similar output than your pipeline above.)

See the "process substitution" section of the "zshexpn" man page for more details.

like image 94
wnoise Avatar answered Oct 22 '22 14:10

wnoise