You probably know to use the
diff file1 file2 command to compare file1 and file2 line by line.
Sometimes you don’t want to compare files, but output of commands. For example, compare
sort file1 and
sort file2. Or maybe compare
grep foo file1 and
grep foo file2.
Of course, there’s the obvious way – run the commands, redirect their output to temp files, and compare the temp files:
$ sort file1 >tmpf1
$ sort file2 >tmpf2
$ diff tmpf1 tmpf2
$ rm tmpf*
But, really..? 3 extra commands just for the one command you really wanted?
There must be a better way! This post is about the better way 🙂 .
Even better, the solution is more general than diff. It allows using the STDOUT of any command as a “file” input for other commands!
Shell-Foo is a series of fun ways to take advantage of the powers of the shell. In the series, I highlight shell one-liners that I found useful or interesting. Most of the entries should work on bash on Linux, OS X and other UNIX-variants. Some probably work with other shells as well. Your mileage may vary.
Feel free to suggest your own Shell-Foo one-liners!