Powershell比较文本文件并显示差异

时间:2013-10-16 11:50:29

标签: powershell

我需要进行文件比较,并希望使用Powershell。我需要一个列出所有差异的输出文件。下面是一个很好的开始,但我需要文件包含行号,并且生成的输入对象当前也会在89个字符后被切断 - 我需要显示完整行:

compare-object (get-content $File1) (get-content $File2) | Out-File $Location

6 个答案:

答案 0 :(得分:10)

输入对象被默认显示截断。要将整行保存到文件中:

compare-object (get-content $File1) (get-content $File2) | format-list | Out-File $Location

答案 1 :(得分:3)

$abc = gc .\z.txt | %{$i = 1} { new-object psobject -prop @{LineNum=$i;Text=$_}; $i++}
$cde = gc .\x.txt | %{$i = 1} { new-object psobject -prop @{LineNum=$i;Text=$_}; $i++}
Compare-Object $abc $cde -Property Text -PassThru -IncludeEqual

尝试使用吐出的行号。

答案 2 :(得分:0)

我使用以下内容将整行保存在文件

Compare-Object -referenceObject $(Get-Content $File1) -differenceObject $(Get-Content $File2) | %{$_.Inputobject + $_.SideIndicator} | ft -auto | out-file $Location -width 5000

答案 3 :(得分:0)

$apples = Get-Content D:\misc\1.txt
$oranges = Get-Content D:\misc\2.txt

Compare-Object -ReferenceObject $apples -DifferenceObject $oranges -PassThru | Out-File D:\misc\mm.csv

答案 4 :(得分:0)

$result= Compare-Object -ReferenceObject $(Get-Content D:\Scalability\misc\1.txt) -DifferenceObject $(Get-Content D:\Scalability\misc\1.txt) | Select -Property InputObject 
$result.InputObject

使用上面的代码获得2个文件的差异

答案 5 :(得分:0)

我使用了这个答案https://serverfault.com/a/951843。在我看来,它提供了更有用的输出:

  • 包含行号
  • 按行号排序
  • 也只使用powershell命令
相关问题