我下面有一个代码,该代码将检查比较的两个差异文件的计数是否将匹配/未匹配。差异确实设置了MISMATCHED结果,但是当两个计数相同时,它确实显示了MATCHED结果,尽管它将在另一列(file1)上生成0计数,而在另一列(file2)上给出其真实值。谁能帮忙确定我的代码遗漏了吗?我只希望它显示正确生成的计数以及结果。
代码:
cls
$f1 = Get-Content "file1.txt" | Select-String "400","901","902","905","906","908","910","920","930"
$f2 = Get-Content "file2.txt"| Select-String "400","901","902","905","906","908","910","920","930"
Compare-Object -ReferenceObject $f1 -DifferenceObject $f2 -IncludeEqual |ForEach-Object {
$i = 0; $ht = @{}; $vals = -split $_.InputObject
foreach($col in 'HEADER', 'COUNT') {
$ht.$col = $vals[$i++]
}
$ht.After = $ht.SideIndicator -eq '=>'
[pscustomobject] $ht
} | Group-Object HEADER | ForEach-Object {
$ndxBefore, $ndxAfter = if ($_.Before) { 0, 1 } else { 1, 0 }
[pscustomobject] @{
HEADER = $_.Group[0].HEADER
'file1 counts' = ($_.Group[$ndxBefore].COUNT, $_.Group[$ndxBefore].Value)[$null -ne $_.Group[$ndxAfter].Value]
'file2 counts' = ($_.Group[$ndxAfter].COUNT, $_.Group[$ndxAfter].Value)[$null -ne $_.Group[$ndxAfter].Value]
'STATUS' = ('MATCH', 'MISMATCH')[$_.Group.Count -gt 1]
}
} | Sort-Object HEADER|
Format-Table HEADER, 'file1', 'file2', 'STATUS'
结果(*中的数字应相同)
HEADER file1 file2 STATUS
-------- ---- ---- ------
400 0 0 MATCH
901 64 53 MISMATCH
902 0 6 MATCH*
905 0 0 MATCH
906 11651 11633 MISMATCH
908 17807 14213 MISMATCH
910 29059 24517 MISMATCH
920 0 0 MATCH
930 0 681 MATCH*