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比較兩個字符串
這適用於我給出的示例,但如果出現不正確的字符,它會返回兩個字符。 Hildred和Mildred返回2時,它應該也會返回1 – Peter3
不知道它是如何知道是否只有一個。如果一個人說肖恩,另一個說吉恩,而這個名字實際上是迪恩,那麼確實有兩個錯誤。 – mjolinor
只是做了一些額外的測試,而且這可能不是一個好的解決方案。它不會捕獲轉置的字符。您可能正在尋找使用您想要的精確匹配規則編寫自定義函數。 – mjolinor