windows .bat 文件比较两个文本文件并输出差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13045754/
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-09 10:00:51  来源:igfitidea点击:

.bat file to compare two text files and output the difference

windows

提问by Martin O Leary

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

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

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

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

1 action
2 action
3 action
4 action 
5 action

and test2.txt looks like

和 test2.txt 看起来像

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

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

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

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.

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

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.

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

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

对不起,我的基本问题,但我用谷歌搜索了这个问题,似乎无法弄清楚。

回答by jacob justin

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

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

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


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

here

这里

/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.

回答by Rahul Tripathi

You can try something like this:-

你可以尝试这样的事情:-

  @echo off
   :main
   fc c:\filename r:\filemame > nul
   if errorlevel 1 goto error

   :next
   echo insert next CD
    pause
  goto main

  :error
  echo failed check

From the source

源头

回答by cowboydan

You might want to check this out:

你可能想看看这个:

http://gnuwin32.sourceforge.net/packages.html

http://gnuwin32.sourceforge.net/packages.html

There are ports of *nix utilities, including one for diff so you won't have to reinvent the wheel.

有 *nix 实用程序的端口,包括一个用于 diff 的端口,因此您不必重新发明轮子。