比较两个具有重复的文本文件,并将明显的差异写入文本文件 [英] Compare two text files with duplicates and write the distinct differences to text file

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

问题描述

我想比较2个文本文件,然后将差异输出到另一个文本文件中.

I want to compare 2 text files and output the difference in another text file.

compare-object (get-content c:\temp\hostname_old.txt) (get-content c:\temp\hostname_new.txt) | Select-Object -ExpandProperty InputObject | Out-File $Location

hostname_old.txt

hostname_old.txt

server02
server05
server04
server06
server01

hostname_new.txt(名称重复)

hostname_new.txt (has duplicate names)

server04
server01
server02
server04
server02

结果:

server04
server02
server05
server06

请注意server04server02在此差异列表中的存在方式,即使它们在 输入文件中也存在.

Note how server04 and server02 are present in this list of differences, even though they're present in both input files.

这就是我想要的:

server05
server06

推荐答案

在比较之前,请使用Select-Object -Unique消除重复项:

Use Select-Object -Unique to eliminate the duplicates before comparing:

compare-object -PassThru `
  (get-content c:\temp\hostname_old.txt) `
  (get-content c:\temp\hostname_new.txt | Select-Object -Unique)

与您上一个问题的此答案相同,-PassThru用于直接传递不同的行,而无需默认情况下,Compare-Object输出的[pscustomobject]包装器(通过其.SideIndicator属性指示差异的源集).

As in this answer to your previous question, -PassThru is used to pass out the differing lines directly, without the [pscustomobject] wrappers (that indicate the source set of the difference via their .SideIndicator property) that Compare-Object outputs by default.

这篇关于比较两个具有重复的文本文件,并将明显的差异写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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