Powershell比较文本文件并显示差异
我需要进行文件比较,并希望使用Powershell。我需要一个输出文件列出所有的差异。下面是一个很好的开始,但我需要文件来输入行号,并且生成的输入对象当前在89个字符后也会被切断-我需要显示完整的行:Powershell比较文本文件并显示差异,powershell,Powershell,我需要进行文件比较,并希望使用Powershell。我需要一个输出文件列出所有的差异。下面是一个很好的开始,但我需要文件来输入行号,并且生成的输入对象当前在89个字符后也会被切断-我需要显示完整的行: compare-object (get-content $File1) (get-content $File2) | Out-File $Location 默认显示将截断输入对象。要将整行保存到文件中,请执行以下操作: compare-object (get-content $File1) (g
compare-object (get-content $File1) (get-content $File2) | Out-File $Location
默认显示将截断输入对象。要将整行保存到文件中,请执行以下操作:
compare-object (get-content $File1) (get-content $File2) | format-list | Out-File $Location
试着用这个方法吐出行号。我用下面的方法将整行保存在一个文件中 Compare-Object -referenceObject $(Get-Content $File1) -differenceObject $(Get-Content $File2) | %{$_.Inputobject + $_.SideIndicator} | ft -auto | out-file $Location -width 5000 比较对象-引用对象$(获取内容$File1)-差异对象$(获取内容$File2)|%{$\输入对象+$\侧指示器}| ft-自动|输出文件$Location-宽度5000 使用上面的代码获得两个文件的差异我使用了这个答案。在我看来,它提供了更有用的输出:
- 包含行号
- 按行号排序
- 也仅使用powershell命令
$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
$result= Compare-Object -ReferenceObject $(Get-Content D:\demo\misc\1.txt) -DifferenceObject $(Get-Content D:\demo\misc\1.txt) | Select -Property InputObject
$result.InputObject