views:

432

answers:

3

I have two files in which some of the lines have changed order. I would like to be able to compare these.

One website suggested something that looks like this:

diff <(sort text2) <(sort text1)

But this yields the error: Missing name for redirect.

I am using tcsh. Is the command above for a different shell?

Is there a better way?

+1  A: 

The problem with your posted 'diff' is that diff can only receive one file via stdin. So I think you'll have to write at least one sorted file to a temporary file.

diff - file.txt

will diff stdin versus a file.txt. The '-' represents stdin

EDIT: I'd assumed that the process substitution would work via stdin. But that's not the case and the above is going via /dev/fd/{num} as pointed out by VardhanDotNet above.

Brian Agnew
+2  A: 

If this does not work for your shell, just do it in 3 lines:

sort text1 > text1.sorted
sort text2 > text2.sorted
diff text1.sorted text2.sorted

Simple but should work...

Matthieu BROUILLARD
+7  A: 

This redirection syntax is bash specific. Thus it won't work in tcsh.

You can call bash and specify the command directly:

bash -c 'diff <(sort text2) <(sort text1)'
David Schmitt
This is a great tip, but I don't fully understand why it works. Could you explain how wrapping it with "bash -c" makes the redirection work?
rooskie
@rooskie: better?
David Schmitt
+1 nice, quick, to-the-point
Will Bickford