Compare two files ignoring order Compare two files ignoring order unix unix

Compare two files ignoring order


Sort the files first:

$ sort file1 > file1.sorted$ sort file2 | diff - file1.sorted

Also, although I personally discourage this sort of thing, if you are using bash and this feature is enabled on your system you can avoid the temporary file by using a process substitution:

$ diff <(sort file1) <(sort file2)


Maybe you're looking at the problem from the wrong side. Perhaps you would like to sort both files and then compare them?

Otherwise,

diff file1 file2 

would do exactly what you are asking for.


Not bash, but still fast way using python:

def check_diff(file1,file2):    check = {}    for file in [file1,file2]:        with open(file,'r') as f:            check[file] = []            for line in f:                check[file].append(line)    diff = set(check[file1]) - set(check[file2])    for line in diff:        print(line.rstrip())