Peter3
Peter3

Reputation: 2679

Powershell Compare Two Strings

 cls
$logFile = "C:\test\output1.txt"
Function LogWrite
{
   Param ([string]$logstring)

   Add-content $Logfile -value $logstring
}
LogWrite "DocumentID|Correct|Wrong|UDI|Number of Errors|Line Number"
LogWrite "------------------------------------------"
$file = "C:\test\test\Birth records evt logging.txt"
$pattern = "^(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(COB Reviewed)$"
$pattern2 = "^(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(DocSecID)$"
$pattern3 = "^(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)$"



$errorCountTotal = 0
$linecount = 0
$line2Count = 0
Get-Content $file|

ForEach-Object{
$errorCountLine = 0
$linecount++
$transposition = $false
   if($_ -match $pattern){


    }elseif($_ -match $pattern2){




    }elseif($_ -match $pattern3){
    $line2Count++



            if($matches[6].Length -eq $matches[7].length){
                 $wrong = $matches[6]
                 $correct = $matches[7]
                 $documentID = $matches[3]
                 $UDI = $matches[8]

                $a = [char[]]$Matches[6]
                $b = [char[]]$matches[7]


               # for($i = 0; $i -lt $a.Length; $i++){

                   # for($x = 1; $x -lt $a.Length; $x++){
                       # if($a[$i] -eq $b[$i+$x] -and $a[$i+$x] -eq $b[$i]){
                         #   if($a[$i] -eq $a[$i+$x]){
                           # write-host "same letter"
                         #   }else{

                               # $errorCountLine += 2

                           # }
                       # }

                   # }


                #}





                #Compare-Object $a $b |Format-List |Out-File "C:\test\test3.txt"

                $errorCountLine += (@(Compare-Object $a $b -SyncWindow 0).count /2)

                $errorCountTotal +=$errorCountLine
                Write-Host $matches[6] " - " $matches[7] " - " $errorCountLine " - " $linecount
                Write-Host $errorCountTotal
                LogWrite "$documentID|$wrong|$correct|$UDI|$errorCountLine|$linecount"
            }else{
                $a = [char[]]$Matches[6]
                $b = [char[]]$matches[7]

                for($i = 0; $i -lt $a.Length; $i++){

                    for($x = 1; $x -lt $a.Length; $x++){
                        if($a[$i] -eq $b[$i+$x] -and $a[$i+$x] -eq $b[$i]){
                            if($a[$i] -eq $a[$i+$x]){
                           # write-host "same letter"
                            }else{

                                $errorCountLine += 2

                            }
                        }

                    }


                }

                $diffL = [math]::Abs($Matches[7].Length - $Matches[6].Length)
                $errorCountLine = (((@(Compare-Object $a $b).count-$diffL) /2) + $diffL)
                $test = @(Compare-Object $a $b).count
                $errorCountTotal += $errorCountLine

                Write-Host $matches[6] " - " $matches[7] " - " $errorCountLine " - " $linecount
                $wrong = $matches[6]
                $correct = $matches[7]
                $documentID = $matches[3]
                $UDI = $matches[8]
                LogWrite "$documentID|$wrong|$correct|$UDI|$errorCountLine|$linecount"
                Write-Host $errorCountTotal

            }


    }



}

Write-Host $line2Count #number of lines that the program looks at.  passes through pattern3.
LogWrite `n
LogWrite "The total number of errors is $errorCountTotal"

I need to compare the contents of two Strings with this program. Above is what i have so far. The only problem is it tells me whether or not the two strings match or not(0 or -1) character by character in the string. Midred and Mildred would come up as 5 errors when in fact it needs to be only 1 error. i cannot just compare the strings as a whole either because there could be multiple errors in a string. any ideas would be great.

Upvotes: 0

Views: 2525

Answers (2)

Mark Bertenshaw
Mark Bertenshaw

Reputation: 5689

Why not just compare the two strings, e.g.

$matches[6] -ne $ matches [7]

?

Upvotes: 0

mjolinor
mjolinor

Reputation: 68243

Try using Compare-Object on the character arrays:

$a = [char[]]'Mildred'
$b = [char[]]'Midred'

Compare-Object $a $b


                                                           InputObject SideIndicator                                                        
                                                           ----------- -------------                                                        
                                                                     l <=                   

@(Compare-Object $a $b).count

1

Upvotes: 1

Related Questions