#navi(../)
* PowerShellでdiffを取る・Compare-Object [#e9976c6e]
PowerShellのdiffはCompare-Objectコマンドレットになります。~
実際にCompare-Objectを使った使用例を以下に記します。
#contents
#htmlinsertpcsp(win-top.html,win-sp.html)
・ファイルの比較をしてみる
比較の構文は以下のようになります。
Compare-Object (Get-Content ファイル) (Get-Content ファイル)
以下のテキストファイルを2つ用意し、Compare-Objectコマンドレットで比較した時の出力です。
-a.txt
Windows 3.1
Windows 95
Windows 2000
Windows Me
Windows XP
Windows Vista
windows 7
Windows 8
Windows 8.1
Windows 10
-b.txt
Windows 3.1
Windows 95
Windows 98
Windows 2000
Windows Me
Windows XP
Windows Vista
Windows 7
Windows 10
**実行結果1(大文字小文字区別なし) [#o86a9f33]
比較結果が以下のように表示されます。~
大文字小文字が区別されていないのがわかります。(windows 7とWindows 7が同じになっている)
PS C:\Users\sakura> Compare-Object (Get-Content a.txt) (Get-Content b.txt)
InputObject SideIndicator
----------- -------------
Windows 98 =>
Windows 8 <=
Windows 8.1 <=
**実行結果2(大文字小文字の区別あり) [#h7eaefe9]
オブションで-CaseSensitiveを追加すれば大文字小文字が区別されます。
PS C:\Users\sakura> Compare-Object -CaseSensitive (Get-Content a.txt) (Get-Content b.txt)
InputObject SideIndicator
----------- -------------
Windows 98 =>
Windows 7 =>
windows 7 <=
Windows 8 <=
Windows 8.1 <=
以上、Compare-Objectコマンドレットを使ったファイル比較方法でした。
#htmlinsertpcsp(win-btm.html,win-sp.html)