bash 忽略顺序比较两个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10708300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:21:50  来源:igfitidea点击:

Compare two files ignoring order

bashunixdiff

提问by RSFalcon7

I have two files which the order of lines is irrelevant, and I want to compare its content.

我有两个文件,行的顺序无关紧要,我想比较其内容。

I looked into diffdocumentation but could not find anything like --ignore-order.

我查看了diff文档,但找不到类似--ignore-order.

Any tips?

有小费吗?

回答by William Pursell

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:

此外,虽然我个人不鼓励这种事情,但如果您使用 bash 并且系统上启用了此功能,则可以通过使用进程替换来避免临时文件:

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

回答by theglauber

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.

会做你所要求的。

回答by mjkapkan

Not bash, but still fast way using python:

不是 bash,但仍然是使用 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())