2014-04-04 65 views
0
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個錯誤。我不能將字符串作爲一個整體進行比較,因爲字符串中可能存在多個錯誤。任何想法都會很棒。Powershell比較兩個字符串

回答

1

嘗試使用的字符數組比較-對象:

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

Compare-Object $a $b 


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

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

1 
+0

這適用於我給出的示例,但如果出現不正確的字符,它會返回兩個字符。 Hildred和Mildred返回2時,它應該也會返回1 – Peter3

+0

不知道它是如何知道是否只有一個。如果一個人說肖恩,另一個說吉恩,而這個名字實際上是迪恩,那麼確實有兩個錯誤。 – mjolinor

+0

只是做了一些額外的測試,而且這可能不是一個好的解決方案。它不會捕獲轉置的字符。您可能正在尋找使用您想要的精確匹配規則編寫自定義函數。 – mjolinor

0

爲什麼不直接比較兩個字符串,例如

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

+0

然後它通過或不通過。如果在字符串前有兩個錯誤/差異會發生什麼。 apdla和蘋果,需要計算兩個錯誤。 – Peter3