使用powershell比较字符串不起作用

时间:2012-10-15 16:51:51

标签: string powershell compare

我正在尝试使用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

等等。

1 个答案:

答案 0 :(得分:1)

如果不访问数据,很难回答。您是否查看了Compare-Object cmdlet?