比较对象 - 单独的侧栏 [英] Compare-Object - Separate side columns
问题描述
是否可以在两列中显示 PowerShell Compare-Object
的结果,以显示引用与不同对象的差异?
Is it possible to display the results of a PowerShell Compare-Object
in two columns showing the differences of reference vs difference objects?
例如使用我当前的 cmdline:
For example using my current cmdline:
Compare-Object $Base $Test
给出:
InputObject SideIndicator
987654 =>
555555 <=
123456 <=
实际上这个列表很长.为了更容易读取数据,可以像这样格式化数据:
In reality the list is rather long. For easier data reading is it possible to format the data like so:
Base Test
555555 987654
123456
所以每列显示该对象中存在哪些元素与其他元素.
So each column shows which elements exist in that object vs the other.
对于奖励积分,像这样在列标题中有一个计数会很棒:
For bonus points it would be fantastic to have a count in the column header like so:
Base(2) Test(1)
555555 987654
123456
推荐答案
可能吗?当然.可行的?没那么多.PowerShell 并不是真正为创建这种表格输出而构建的.您可以做的是通过输入文件将哈希表中的差异收集为嵌套数组:
Possible? Sure. Feasible? Not so much. PowerShell wasn't really built for creating this kind of tabular output. What you can do is collect the differences in a hashtable as nested arrays by input file:
$ht = @{}
Compare-Object $Base $Test | ForEach-Object {
$value = $_.InputObject
switch ($_.SideIndicator) {
'=>' { $ht['Test'] += @($value) }
'<=' { $ht['Base'] += @($value) }
}
}
然后转置哈希表:
$cnt = $ht.Values |
ForEach-Object { $_.Count } |
Sort-Object |
Select-Object -Last 1
$keys = $ht.Keys | Sort-Object
0..($cnt-1) | ForEach-Object {
$props = [ordered]@{}
foreach ($key in $keys) {
$props[$key] = $ht[$key][$_]
}
New-Object -Type PSObject -Property $props
} | Format-Table -AutoSize
要在标题名称中包含项目计数,请将 $props[$key]
更改为 $props["$key($($ht[$key].Count))"]
.
To include the item count in the header name change $props[$key]
to $props["$key($($ht[$key].Count))"]
.
这篇关于比较对象 - 单独的侧栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!