【问题标题】:.bat file to compare two text files and output the difference.bat 文件比较两个文本文件并输出差异
【发布时间】:2018-04-13 22:57:06
【问题描述】:

我正在尝试一些我在 UNIX 上成功完成但不知道如何在 Windows 上完成的新事物。

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

1 action
2 action
3 action
4 action 
5 action

和 test2.txt 看起来像

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

那么第三个文件 diff.txt 的输出将如下所示:

6 action
7 action
8 action

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

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

很抱歉我的基本问题,但我已经用谷歌搜索了这个问题,但似乎无法弄清楚。

【问题讨论】:

    标签: windows


    【解决方案1】:

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

    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.
    

    【讨论】:

    • 正在打印与 Rignt 手边文件不匹配的内容。
    【解决方案2】:

    你可以试试这样的:-

      @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
    

    来自source

    【讨论】:

      【解决方案3】:

      你可能想看看这个:

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

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

      【讨论】:

        猜你喜欢
        • 2022-10-13
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 2017-12-11
        • 1970-01-01
        • 2014-06-08
        相关资源
        最近更新 更多