What is an easy way to do a sorted diff between two files?
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?
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)'
Here's a function for it:
function diffs() {
diff "${@:3}" <(sort "$1") <(sort "$2")
}
Call it like this:
diffs file1 file2 [other diff args, e.g. -y]
Presumably you could alter it as per David Schmitt's answer if necessary.
Is there a better way?
Yes, there is.
Use comm
utility:
usage: comm [-123i] file1 file2
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...
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.