Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active February 20, 2025 21:56
Show Gist options
  • Save jdhitsolutions/71efaf9df48cbfc0429ed6c1713e9cec to your computer and use it in GitHub Desktop.
Save jdhitsolutions/71efaf9df48cbfc0429ed6c1713e9cec to your computer and use it in GitHub Desktop.

Revisions

  1. jdhitsolutions revised this gist Feb 20, 2025. 2 changed files with 101 additions and 1 deletion.
    101 changes: 101 additions & 0 deletions Compare-File.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    #requires -version 7.2
    <#
    this is a variation of what you could do comparing text files
    using Compare-Object. The only difference really is how this
    script formats the output.
    This should be considered a PROOF-OF-CONCEPT
    #>

    Param(
    [Parameter(
    Position = 0,
    Mandatory,
    HelpMessage = 'Reference file to compare against'
    )]
    [ValidateScript({ Test-Path $_ }, ErrorMessage = 'File {0} cannot be found.')]
    [Alias("rf")]
    [string]$ReferenceFile,
    [Parameter(
    Position = 1,
    Mandatory,
    HelpMessage = 'The Difference file to compare'
    )]
    [ValidateScript({ Test-Path $_ }, ErrorMessage = 'File {0} cannot be found.')]
    [Alias("df")]
    [string]$DifferenceFile,
    [Parameter(HelpMessage = 'Use a case-sensitive comparison')]
    [Alias("cs")]
    [Switch]$CaseSensitive
    )

    #a private helper function to display the differences using an ANSI sequence
    Function HighlightDiff {
    Param(
    [string]$a,
    [string]$b,
    [switch]$CaseSensitive
    )
    #write-Host "Comparing $a to $b" -ForegroundColor Magenta
    [regex]$rx = '\b\S+\b'
    $aWords = $rx.Matches($a).Value
    $bWords = $rx.Matches($b).Value
    if ($aWords -AND $bWords) {
    $differentWords = Compare-Object $aWords $bWords -CaseSensitive:$CaseSensitive |
    Where-Object SideIndicator -EQ '=>' |
    Select-Object -ExpandProperty InputObject

    # Highlight different words in $b
    $output = $b
    foreach ($word in $differentWords) {
    $output = $output -replace "\b$word\b", "`e[38;5;213m$word`e[0m"
    }
    $output
    }
    }

    $ref = [System.Collections.Generic.List[string]](Get-Content $ReferenceFile)
    $diff = [System.Collections.Generic.List[string]](Get-Content $DifferenceFile)

    for ($i = 0; $i -le $ref.count; $i++) {
    if ($CaseSensitive) {
    $test = $diff[$i] -cne $ref[$i]
    }
    else {
    $test = $diff[$i] -ne $ref[$i]
    }
    if ($test) {
    [PSCustomObject]@{
    PSTypeName = 'FileComparison'
    Line = $i + 1
    Ref = $ref[$i]
    Comparison = '=>'
    Diff = HighlightDiff -a $ref[$i] -b $diff[$i] -CaseSensitive:$CaseSensitive
    }
    }
    }
    if ($diff.count -gt $ref.count) {
    #write-host "get extra lines from diff file" -ForegroundColor cyan
    for ($i = $ref.count+1; $i -le $diff.count; $i++) {
    [PSCustomObject]@{
    PSTypeName = 'FileComparison'
    Line = $i + 1
    Ref = $Null
    Comparison = '=>'
    Diff = $diff[$i]
    }
    }
    }
    elseif ($ref.count -gt $diff.count) {
    #write-host "get extra lines from ref file" -ForegroundColor green
    for ($i = $diff.count+1; $i -le $ref.count; $i++) {
    [PSCustomObject]@{
    PSTypeName = 'FileComparison'
    Line = $i + 1
    Ref = $ref[$i]
    Comparison = '<='
    Diff = $Null
    }
    }
    }
    1 change: 0 additions & 1 deletion To Have and Have Not, 1944
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    You know how to whistle, don't you, Steve? You just put your lips together and blow.
  2. jdhitsolutions created this gist Feb 20, 2025.
    1 change: 1 addition & 0 deletions To Have and Have Not, 1944
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    You know how to whistle, don't you, Steve? You just put your lips together and blow.