How can i reorganise powershell's compare-object output?

佐手、 提交于 2019-12-24 07:46:32

问题


I'm comparing 2 CSV with the compare-object cmdlet :

Compare-object $CSV1 $CSV2 -property Header, Value -passthru | sort-object Header

After comparing 2 CSVs, i ended up with the following output :

Header              Value           SideIndicator
-------             -----           -------------
String1             Value 1             <=
String1             Value 2             =>
String2             Value 3             <=
String2             Value 4             =>
String3             Value 5             =>
String4             Value 6             <=

I'd like to reorganise it to the following format

Header1             Old Value       New Value
------              ---------       ---------
String1             Value 1         Value 2
String2             Value 3         Value 4
String3             NA              Value 5
String4             Value 6         NA

Is that doable in powershell ?


回答1:


You can try this :

$a = Compare-Object (Import-Csv 'C:\temp\f1.csv')  (Import-Csv 'C:\temp\f2.csv')  -property Header,Value
$a | Group-Object -Property Header | % { New-Object -TypeName psobject -Property @{Header=$_.name;newValue=$_.group[0].Value;oldValue=$_.group[1].Value}}


来源:https://stackoverflow.com/questions/17446034/how-can-i-reorganise-powershells-compare-object-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!