0

我正在尝试使用 powershell 比较字符串,但它没有按要求工作。

$compare_test = @(Get-Content compare_test.txt)
$test = @(Get-Content test.txt)
for ($c1 = 0; $c1 -lt $test.Length; $c1++) {
    $space_position = $test[$c1].IndexOf(" ")
    $leftpart = $test[$c1].Substring(0, $space_position)
    $rightpart = $test[$c1].Substring($space_position+1)
    $type_cast_rightpart = [int]$rightpart
    for ($c3 = 0; $c3 -lt $compare_test.Length; $c3++) {
        $space_position_2 = $compare_test[$c3].IndexOf(" ")
        $leftpart_2 = $compare_test[$c3].Substring(0, $space_position_2)
        $rightpart_2 = $compare_test[$c3].Substring($space_position_2+1)
        $type_cast_rightpart_2 = [int]$rightpart_2
        $same_server = $leftpart_2.CompareTo($leftpart)
        if ($same_server -eq 0) {
            if ($type_cast_rightpart -gt $type_cast_rightpart_2) {
                $entry_server_log = $leftpart + " " + $type_cast_rightpart
                $myarray += $entry_server_log
                break
            }
            else {
                $entry_server_log = $leftpart + " " + $type_cast_rightpart_2
                $myarray += $entry_server_log
                break
            }
        }
        elseif ($same_server -eq 1 -or $same_server -eq -1) {
            $entry_server_log = $leftpart_2 + " " + $type_cast_rightpart_2
            $myarray += $entry_server_log
            break
        }
    }
}
Set-Content compare_test.txt $myarray

每次我尝试运行 powershell 脚本时,elseif 部分都会执行。test.txt 和 compare_test 都包含相同的字符串集,如下所示

NTHKGTBP126 2401
NTHKGTBP132 806

等等。

4

1 回答 1

1

没有访问数据很难回答。你看过Compare-Objectcmdlet 吗?

于 2012-10-15T17:26:05.900 回答