【问题标题】:Comparing/finding the difference between two text files using findstr使用 findstr 比较/​​查找两个文本文件之间的差异
【发布时间】:2014-07-28 14:41:17
【问题描述】:

我需要比较两个文本文件并找出它们之间的区别。基本上我有一个输入文件(input.txt),它将由批处理作业处理,我的批处理将记录作业成功运行的输出(successful.txt)。

简单来说,我需要找出input.txt和successful.txt(input.txt-successful.txt)之间的区别,我正在考虑使用findstr。这似乎很好,但我不明白其中的一部分。它总是在输出中包含我的 input.txt 的最后一行。您可以在下面的示例中看到这一点。请注意,在我的 input.txt 的最后一行之后没有前导空格或换行符。

在下面的示例中,您可以看到 server1,db1 行出现在两个文件中,但仍列在输出中。 (始终是 input.txt 的最后一行)

D:\Scripts\dummy>type input.txt
server2,db2
server3,db3
server10,db10
server4,db4
server1,db11
server10,schema11
host1,sch2
host11,sql2
host11,sql3
server1,db1
D:\Scripts\dummy>type successful.txt
server1,db1
server2,db2
server3,db3
server4,db4
server10,db10
host1,sch2
host11,sql2
host11,sql3
D:\Scripts\dummy>findstr /vixg:successful.txt input.txt
server1,db11
server10,schema11
server1,db1

我做错了什么?

干杯, G

【问题讨论】:

  • 在 input.txt 的末尾添加一个 CR。正如dbenham here. 所解释的那样

标签: windows batch-file command-line cmd findstr


【解决方案1】:

我可以通过删除input.txt 的最后一行之后的换行符来重现您的结果,因此解决方案 1 是在input.txt 的末尾添加一个换行符。既然您似乎说 input.txt 没有终端换行符,那么添加一个就可以解决问题; findstr 的行为符合预期,因为它作用于换行符终止的行。

解决方案 2 是

type input.txt|findstr /vixg:successful.txt

【讨论】:

  • 酷,我不知道这个-findstr is acting as expected because it acts on newline-terminated lines. 也感谢您的替代解决方案!
猜你喜欢
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 2017-12-11
  • 2023-03-07
  • 2010-11-23
  • 2011-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多