Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an easy way to do a sorted diff between two files?

Tags:

shell

unix

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?

like image 394
mmccoo Avatar asked Apr 03 '09 15:04

mmccoo


People also ask

What is command to compare the sorted files?

To compare two sorted files, we use the comm command in the Linux system. The comm command is used to compare two sorted files line by line and writes three columns to standard output.


2 Answers

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)' 
like image 85
David Schmitt Avatar answered Oct 20 '22 03:10

David Schmitt


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.

like image 36
hajamie Avatar answered Oct 20 '22 03:10

hajamie