.bat 文件比较两个文本文件并输出差异 [英] .bat file to compare two text files and output the difference

查看:76
本文介绍了.bat 文件比较两个文本文件并输出差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些我在 UNIX 上成功完成的新东西,但不知道如何在 Windows 上做.

I am trying something new that I have done on UNIX successfully but have no idea how to do on windows.

所以我保存了一个文本文件,假设 test1.txt 和 12 小时后比较 test2.txt(这是 test1.txt,在 12 小时内添加了更改,几乎可以保证在文件末尾)test1.txt 然后只将文本差异输出到第三个文件 diff.txt

So I save a text file, let's say test1.txt and 12 hours later compare the test2.txt (which is test1.txt with changes added during the 12 hours, almost guaranteed to be at the end of the file) to test1.txt and then output just the text differences to a third file, diff.txt

1 action
2 action
3 action
4 action 
5 action

和 test2.txt 看起来像

and test2.txt looks like

1 action
2 action
3 action
4 action 
5 action
6 action
7 action
8 action

然后输出到第三个文件 diff.txt 将如下所示:

then the output to the third file diff.txt would look like:

6 action
7 action
8 action

只有添加的文本,没有关于行或比较的信息,只是差异的基本输出.

with just the text that has been added, no info regarding lines or comparisons,just a basic output of the differences.

我对此完全陌生,环顾四周,似乎我可以编写一个批处理文件 (.bat),它基本上就像 UNIX 脚本一样.

I am COMPLETELY new to this, have looked around and it seems I can write a batch file (.bat) that will basically just act as a UNIX script would.

抱歉我的基本问题,但我在谷歌上搜索了这个问题,但似乎无法弄清楚.

Sorry for my basic question but I've googled the question and can't seem to figure it out.

推荐答案

最简单最快的方法是使用 findstr 命令,它会比较结果并将结果返回到新文件中的脚本

The Simplest and fastest method is using findstr command it will compare and return the result to new file here the script

findstr /vixg:Z:\misc\test1.txt Z:\misc\misc\test2.txt > Z:\misc\misc\test3.txt


findstr /vixg:<source file> <target file> > outputfile

这里

/v   : Prints only lines that do not contain a match.
/i   : Specifies that the search is not to be case-sensitive.
/x   : Prints lines that match exactly.
/g: file   : Gets search strings from the specified file.

这篇关于.bat 文件比较两个文本文件并输出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆