Powershell比较两个字符串

时间:2014-04-04 14:52:08

标签: powershell

 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"

我需要将两个字符串的内容与此程序进行比较。以上就是我到目前为止所做的。唯一的问题是它告诉我两个字符串是否在字符串中逐个字符匹配(0或-1)。 Midred和Mildred会出现5个错误,实际上它只需要1个错误。我不能只是将字符串作为一个整体进行比较,因为字符串中可能存在多个错误。任何想法都会很棒。

2 个答案:

答案 0 :(得分:1)

尝试在字符数组上使用Compare-Object:

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

Compare-Object $a $b


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

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

1

答案 1 :(得分:0)

为什么不比较两个字符串,例如

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