サーバー作業をしているとツールがほしくなる時ってありますよね。
今回は PowerShell だけで利用可能な Diff 関数を作ってみました。
以下のコマンドを PowerShell にコピペしてください。
あとは diff-files -fileA ファイル名A -fileB ファイル名B という感じに使っていきます。
出力は3列。
行番号と A B どちらにファイルが文字があったのか。( <= が A 側です)
そしてデータです。
サーバーでは使えるツールががないということが往々にしてあるので、自作しておくと何かと役に立つと思います。
是非ともお試しあれ!
function diff-files{
param([parameter(mandatory=$true)][string]$fileA, [parameter(mandatory=$true)][string]$fileB)
diff -ReferenceObject @(Get-Content -Path (isNullorEmptyandTrim -paramA $fileA)) `
-DifferenceObject @(Get-Content -Path (isNullorEmptyandTrim -paramA $fileB)) |
Sort -Property @{Expression = {$_.InputObject.ReadCount}; Ascending = $true} |
ft -Property @{Name = 'RowNo'; Expression = {$_.InputObject.ReadCount}},
@{Name = 'Indicator'; Expression = {$_.SideIndicator}},
@{Name = 'Data'; Expression = {$_.InputObject}}
}
function isNullorEmptyandTrim{
[OutputType([String])]
param([parameter(mandatory=$true)][string]$paramA)
if([string]::IsNullOrEmpty($paramA)){
return [string]::Empty;
}else{
return $paramA.trim("""");
}
}
音楽:怨狼の牙