如何比较2个文件中的数据以识别常见和唯一的数据?
如何比较2个文件中的数据以识别常见和唯一数据? 我不能一行一行地做,因为我有文件1,其中包含100个id / codes / number-set,我想将文件2与文件1进行比较。
问题是文件2包含文件1中的数据子集以及文件2唯一的数据,例如:
file 1 file 2 1 1 2 a 3 2 4 b 5 3 6 c
如何比较两个文件以识别每个文件的常见和唯一数据? diff
似乎无法完成这项工作。
这是comm
的用途:
$ comm <(sort file1) <(sort file2) 1 2 3 4 5 6 a b c
第一列是仅出现在文件1中的行
第二列是仅出现在文件2中的行
第三列是两个文件共有的行
comm
需要对输入文件进行排序
要排除显示任何列,请添加具有该列编号的选项。 例如,要仅查看共同的行,请使用comm -12 ...
或仅在file2中的行, comm -13 ...
无论您的file1和file2是否已排序,请使用awk命令,如下所示:
file1中的唯一数据:
awk 'NR==FNR{a[$0];next}!($0 in a)' file2 file1 4 5 6
file2中的唯一数据:
awk 'NR==FNR{a[$0];next}!($0 in a)' file1 file2 a b c
常见数据:
awk 'NR==FNR{a[$0];next} ($0 in a)' file1 file2 1 2 3
说明:
NR==FNR - Execute next block for 1st file only a[$0] - Create an associative array with key as '$0' (whole line) and copy that into it as its content. next - move to next row ($0 in a) - For each line saved in `a` array: print the common lines from 1st and 2nd file "($0 in a)' file1 file2" or unique lines in 1st file only "!($0 in a)' file2 file1" or unique lines in 2nd file only "!($0 in a)' file1 file2"
如果您只需要以图形方式查看两个文件(或目录!)之间的更改,则xxdiff
是无与伦比的:
像常规的diff
和comm
,你的输入文件应该先排序。
sort file1.txt > file1.txt.sorted sort file2.txt > file2.txt.sorted xxdiff file1.txt.sorted file2.txt.sorted