比較-オブジェクトの複数の値の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
相違:
- チーム3にゴンザレスとホールを追加する
- チーム4の白を削除
$1 = Import-Csv -Path ".\File1.csv" -Delimiter ',' $2 = Import-Csv -Path ".\File2.csv" -Delimiter ','
Compare-Object $1 $2 -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 <=
回答
2 robdy
最初にオブジェクトを比較して相違点を見つけ(異なるチームのメンバーシップが一致する場合にエントリが欠落しないように、チームとメンバーの2つのプロパティを比較することに注意してください)、次に一致するオブジェクトから作成された配列を比較します。
$1 = Import-Csv -Path ".\File1.csv" -Delimiter ',' $2 = Import-Csv -Path ".\File2.csv" -Delimiter ','
$comparisonRes = Compare-Object $1 $2 -Property Teams,Members -PassThru foreach ($obj in $comparisonRes | Where-Object SideIndicator -eq "=>") { # $obj = ($comparisonRes | Where-Object SideIndicator -eq "=>")[0] $matchingEntry = $1 | 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
}
js2010
配列と数値をサポートするcsvの代わりにjsonを使用することをお勧めします。それ以外の場合、チームは2つのセミコロンで区切られた文字列のように見えます。
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"]}
]
$1 = cat file1.json | convertfrom-json $2 = cat file2.json | convertfrom-json
Compare-Object $1 $2 -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} <=
これがより近い答えです。メンバーに対して一度に1行だけcompare-objectを実行し、チームとカテゴリを追加します。
$1 = cat file1.json | convertfrom-json $2 = cat file2.json | convertfrom-json
for($i = 0; $i -lt $1.length; $i++) {
compare-object $1[$i].members $2[$i].members |
select @{n='Teams'; e={$1[$i].teams}},
@{n='Category'; e={$1[$i].Category}},
@{n='Members'; e={$_.inputobject}},
sideindicator
}
Teams Category Members SideIndicator
----- -------- ------- -------------
Team3 E Gonzalez =>
Team3 E Hall =>
Team4 A White <=
zip関数PowerShell / CLIを使用する別の方法は次のとおりです。オブジェクトの両方のリストに複数の配列がある「Foreach」ループ。
$1 = cat file1.json | convertfrom-json
$2 = 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 $1 $2 | % { $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 <=