linux下查找2个文件的不同 |
您所在的位置:网站首页 › linux类型文件相同和不同点分别 › linux下查找2个文件的不同 |
方法一:comm命令实现 首先看看comm命令的帮助 comm --help Usage: comm [OPTION]... FILE1 FILE2 Compare sorted files FILE1 and FILE2 line by line. -1 suppress column 1 (lines unique to FILE1) -2 suppress column 2 (lines unique to FILE2) -3 suppress column 3 (lines that appear in both files) Examples: comm -12 file1 file2 Print only lines present in both file1 and file2. comm -3 file1 file2 Print lines in file1 not in file2, and vice versa. -1 不显示只在第1个文件里出现过的列。 -2 不显示只在第2个文件里出现过的列。 -3 不显示只在第1和第2个文件里出现过的列。 --help 在线帮助。 --version 显示版本信息。 例子 comm - 12 就只显示在两个文件中都存在的行; comm - 23 只显示在第一个文件中出现而未在第二个文件中出现的行; comm - 123 则什么也不显示。
注意:两个文件比较前,一定要进行sort排序。不然输出的结果可能是错的。
方法二:使用diff命令比较 diff命令是一个比较经典的文本比较工具了,diff命令较comm的参数要多。其经常和patch命令组合使用,进行补丁升级。其默认使用的是-a参数,即逐行比较两个文件之间的不同。此处我们要实现想要得到的结果,还需要配合grep和awk实用: diff 2.txt 1.txt |grep " |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |