Skip to content

Instantly share code, notes, and snippets.

@axjack
Created November 28, 2019 13:28
Show Gist options
  • Save axjack/3c58376824fe98a5ae5a1f1c85c243d2 to your computer and use it in GitHub Desktop.
Save axjack/3c58376824fe98a5ae5a1f1c85c243d2 to your computer and use it in GitHub Desktop.
powershellでdiffを取得する、Compare-Objectを使って。
# 比較用CSVをインポートする
$test1 = Import-Csv -Path test1.csv -Encoding Default
$test2 = Import-Csv -Path test2.csv -Encoding Default
# 比較用項目を指定する
$test1_name = $test1 | %{ $_.name }
$test2_name = $test2 | %{ $_.name }
function Get-MyDiffKey {
param(
$ref
,$dif
,$sideind = "=>"
)
Compare-Object -ReferenceObject $ref -DifferenceObject $dif |`
where {$_.SideIndicator -eq "$sideind" } |`
% { $_.InputObject }
}
# 実行
$my_key = Get-MyDiffKey -ref $test1_name -dif $test2_name
if( $my_key.Count -gt 0 ){
$test2 |% { if($_.name -in $my_key){$_ } }
}else {
Write-Output "..."
}
@axjack
Copy link
Author

axjack commented Nov 28, 2019

PS C:\Users\Satoaki NOGUCHI> cat test[12].csv
id, name
1, 埼玉
2, 千葉
3, 東京
4, 神奈川
id, name
1, 埼玉
2, 千葉
3, 東京
4, 神奈川
5, 茨城
6, 栃木

@axjack
Copy link
Author

axjack commented Nov 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment