使用批处理比较两个文件

时间:2013-07-03 07:39:21

标签: batch-file

我有两个文件file1.txt和file2.txt

File1.txt:

abc
def
xyz

File2.txt

abc

比较file1.txt和file2.txt并创建两个文件,一个有差异,另一个文件显示相似之处。 任何人都可以帮我解决这样的批处理程序。

1 个答案:

答案 0 :(得分:2)

GNU 的Windows代码:

awk "NR==FNR {a[$0]=$0; next}; {b[$0]=$0};a[$0]==$0;END {for (i in a) {if (b[i]==0) {print a[i] >\"diff.txt\"}};for (i in b) {if (a[i]==0) {print b[i]>>\"diff.txt\"}}}" file1 file2

输出示例:

>type file1,file2

file1


AAAAA
44444
BBBBB
55555
CCCCC
66666
DDDDD

file2


AAAAA
11111
BBBBB
22222
CCCCC
33333
DDDDD

>awk "NR==FNR {a[$0]=$0; next}; {b[$0]=$0};a[$0]==$0;END {for (i in a) {if (b[i]==0) {print a[i] >\"diff.txt\"}};for (i in b) {if (a[i]==0) {print b[i]>>\"diff.txt\"}}}" file1 file2>match.txt

>type match.txt,diff.txt

match.txt


AAAAA
BBBBB
CCCCC
DDDDD

diff.txt


44444
55555
66666
11111
22222
33333

>

纯粹

findstr /l /g:file1 file2>match.txt
findstr /lv /g:file1 file2>diff.txt
findstr /lv /g:file2 file1>>diff.txt