比较对象多值 powershell
Compare-Object multi values powershell
拜托,你能帮我找到一个解决方案来处理包含多个字段列的 csv 文件吗
File1.csv
Teams,Category,Members
Team1,A,Smith;Johnson
Team1,C,Jones;Miller;Garcia
Team3,E,Wilson;Martinez
Team4,A,Martin;Jackson;White;Williams
File2.csv
Teams,Category,Members
Team1,A,Smith;Johnson
Team2,C,Jones;Miller;Garcia
Team3,E,Wilson;Martinez;Gonzalez;Hall
Team4,A,Martin;Jackson;Williams
差异:
- 将 Gonzalez 和 Hall 添加到第 3 队
- 移除 Team-4 的白色
= Import-Csv -Path ".\File1.csv" -Delimiter ','
= Import-Csv -Path ".\File2.csv" -Delimiter ','
Compare-Object -Property Members -PassThru
结果:
Teams Category Members SideIndicator
Team3 E Wilson;Martinez;Gonzalez;Hall =>
Team4 A Martin;Jackson;Williams =>
Team3 E Wilson;Martinez <=
Team4 A Martin;Jackson;White;Williams <=
预期结果:
Teams Category Members SideIndicator
Team3 E Gonzalez and Hall =>
Team4 A White <=
我会先比较对象以找出差异(注意我比较两个属性:Teams 和 Members 以避免在不同团队的成员匹配的情况下丢失条目)然后比较从匹配对象创建的数组:
= Import-Csv -Path ".\File1.csv" -Delimiter ','
= Import-Csv -Path ".\File2.csv" -Delimiter ','
$comparisonRes = Compare-Object -Property Teams,Members -PassThru
foreach ($obj in $comparisonRes | Where-Object SideIndicator -eq "=>") {
# $obj = ($comparisonRes | Where-Object SideIndicator -eq "=>")[0]
$matchingEntry = | Where-Object {$_.Teams -eq $obj.Teams}
$matchingEntryMembers = $matchingEntry.Members -split ";"
$currentEntryMembers = $obj.Members -split ";"
$diffMembers = Compare-Object $matchingEntryMembers $currentEntryMembers
# Uncomment to log
# $diffMembers
# Do something with $diffMembers here
}
您可能想使用 json 而不是支持数组和数字的 csv。否则团队看起来像两个分号分隔的字符串。
file1.json
[
{"Teams":"Team1","Category":"A","Members":["Smith","Johnson"]},
{"Teams":"Team1","Category":"C","Members":["Jones","Miller","Garcia"]},
{"Teams":"Team3","Category":"E","Members":["Wilson","Martinez"]},
{"Teams":"Team4","Category":"A","Members":["Martin","Jackson","White","Williams"]}
]
file2.json
[
{"Teams":"Team1","Category":"A","Members":["Smith","Johnson"]},
{"Teams":"Team2","Category":"C","Members":["Jones","Miller","Garcia"]},
{"Teams":"Team3","Category":"E","Members":["Wilson","Martinez","Gonzalez","Hall"]},
{"Teams":"Team4","Category":"A","Members":["Martin","Jackson","Williams"]}
]
= cat file1.json | convertfrom-json
= cat file2.json | convertfrom-json
Compare-Object -Property Members -PassThru
Teams Category Members SideIndicator
----- -------- ------- -------------
Team3 E {Wilson, Martinez, Gonzalez, Hall} =>
Team4 A {Martin, Jackson, Williams} =>
Team3 E {Wilson, Martinez} <=
Team4 A {Martin, Jackson, White, Williams} <=
这里有一个更详细的答案。 运行 一次只比较成员上的对象,然后向其添加团队和类别。
= cat file1.json | convertfrom-json
= cat file2.json | convertfrom-json
for($i = 0; $i -lt .length; $i++) {
compare-object [$i].members [$i].members |
select @{n='Teams'; e={[$i].teams}},
@{n='Category'; e={[$i].Category}},
@{n='Members'; e={$_.inputobject}},
sideindicator
}
Teams Category Members SideIndicator
----- -------- ------- -------------
Team3 E Gonzalez =>
Team3 E Hall =>
Team4 A White <=
这是在两个对象列表上使用 zip 函数 PowerShell/CLI: "Foreach" loop with multiple arrays 的另一种方法。
= cat file1.json | convertfrom-json
= cat file2.json | convertfrom-json
function Zip($a1, $a2) { # function allows it to stream
while ($a1) {
$x, $a1 = $a1 # $a1 gets the tail of the list
$y, $a2 = $a2
[tuple]::Create($x, $y)
}
}
zip | % {
$whole = $_ # will lose this $_ in the select
compare-object $whole.item1.members $whole.item2.members |
select @{n='Teams'; e={$whole.item1.teams}},
@{n='Category'; e={$whole.item1.Category}},
inputobject,sideindicator
}
Teams Category InputObject SideIndicator
----- -------- ----------- -------------
Team3 E Gonzalez =>
Team3 E Hall =>
Team4 A White <=
拜托,你能帮我找到一个解决方案来处理包含多个字段列的 csv 文件吗
File1.csv
Teams,Category,Members
Team1,A,Smith;Johnson
Team1,C,Jones;Miller;Garcia
Team3,E,Wilson;Martinez
Team4,A,Martin;Jackson;White;Williams
File2.csv
Teams,Category,Members
Team1,A,Smith;Johnson
Team2,C,Jones;Miller;Garcia
Team3,E,Wilson;Martinez;Gonzalez;Hall
Team4,A,Martin;Jackson;Williams
差异:
- 将 Gonzalez 和 Hall 添加到第 3 队
- 移除 Team-4 的白色
= Import-Csv -Path ".\File1.csv" -Delimiter ','
= Import-Csv -Path ".\File2.csv" -Delimiter ','
Compare-Object -Property Members -PassThru
结果:
Teams Category Members SideIndicator
Team3 E Wilson;Martinez;Gonzalez;Hall =>
Team4 A Martin;Jackson;Williams =>
Team3 E Wilson;Martinez <=
Team4 A Martin;Jackson;White;Williams <=
预期结果:
Teams Category Members SideIndicator
Team3 E Gonzalez and Hall =>
Team4 A White <=
我会先比较对象以找出差异(注意我比较两个属性:Teams 和 Members 以避免在不同团队的成员匹配的情况下丢失条目)然后比较从匹配对象创建的数组:
= Import-Csv -Path ".\File1.csv" -Delimiter ','
= Import-Csv -Path ".\File2.csv" -Delimiter ','
$comparisonRes = Compare-Object -Property Teams,Members -PassThru
foreach ($obj in $comparisonRes | Where-Object SideIndicator -eq "=>") {
# $obj = ($comparisonRes | Where-Object SideIndicator -eq "=>")[0]
$matchingEntry = | Where-Object {$_.Teams -eq $obj.Teams}
$matchingEntryMembers = $matchingEntry.Members -split ";"
$currentEntryMembers = $obj.Members -split ";"
$diffMembers = Compare-Object $matchingEntryMembers $currentEntryMembers
# Uncomment to log
# $diffMembers
# Do something with $diffMembers here
}
您可能想使用 json 而不是支持数组和数字的 csv。否则团队看起来像两个分号分隔的字符串。
file1.json
[
{"Teams":"Team1","Category":"A","Members":["Smith","Johnson"]},
{"Teams":"Team1","Category":"C","Members":["Jones","Miller","Garcia"]},
{"Teams":"Team3","Category":"E","Members":["Wilson","Martinez"]},
{"Teams":"Team4","Category":"A","Members":["Martin","Jackson","White","Williams"]}
]
file2.json
[
{"Teams":"Team1","Category":"A","Members":["Smith","Johnson"]},
{"Teams":"Team2","Category":"C","Members":["Jones","Miller","Garcia"]},
{"Teams":"Team3","Category":"E","Members":["Wilson","Martinez","Gonzalez","Hall"]},
{"Teams":"Team4","Category":"A","Members":["Martin","Jackson","Williams"]}
]
= cat file1.json | convertfrom-json
= cat file2.json | convertfrom-json
Compare-Object -Property Members -PassThru
Teams Category Members SideIndicator
----- -------- ------- -------------
Team3 E {Wilson, Martinez, Gonzalez, Hall} =>
Team4 A {Martin, Jackson, Williams} =>
Team3 E {Wilson, Martinez} <=
Team4 A {Martin, Jackson, White, Williams} <=
这里有一个更详细的答案。 运行 一次只比较成员上的对象,然后向其添加团队和类别。
= cat file1.json | convertfrom-json
= cat file2.json | convertfrom-json
for($i = 0; $i -lt .length; $i++) {
compare-object [$i].members [$i].members |
select @{n='Teams'; e={[$i].teams}},
@{n='Category'; e={[$i].Category}},
@{n='Members'; e={$_.inputobject}},
sideindicator
}
Teams Category Members SideIndicator
----- -------- ------- -------------
Team3 E Gonzalez =>
Team3 E Hall =>
Team4 A White <=
这是在两个对象列表上使用 zip 函数 PowerShell/CLI: "Foreach" loop with multiple arrays 的另一种方法。
= cat file1.json | convertfrom-json
= cat file2.json | convertfrom-json
function Zip($a1, $a2) { # function allows it to stream
while ($a1) {
$x, $a1 = $a1 # $a1 gets the tail of the list
$y, $a2 = $a2
[tuple]::Create($x, $y)
}
}
zip | % {
$whole = $_ # will lose this $_ in the select
compare-object $whole.item1.members $whole.item2.members |
select @{n='Teams'; e={$whole.item1.teams}},
@{n='Category'; e={$whole.item1.Category}},
inputobject,sideindicator
}
Teams Category InputObject SideIndicator
----- -------- ----------- -------------
Team3 E Gonzalez =>
Team3 E Hall =>
Team4 A White <=