替换文件中的多行

时间:2017-11-03 17:23:45

标签: windows powershell powershell-v2.0 powershell-v3.0

我尝试制作,第一个数组中的行从文件中读取,并替换为第二个数组中的一行,因此有时会使用不同的行。我制作了一个剧本,但我不明白为什么它不起作用。

$OldStrings = @(
    "desktopwidth:i:1440",
    "desktopheight:i:900",
    "winposstr:s:0,1,140,60,1596,999"
)
$NewStrings = @(
    "desktopwidth:i:1734",
    "desktopheight:i:990",
    "winposstr:s:0,1,50,7,1800,1036"
)

$LinesArray = Get-Content -Path 'C:\temp\My Copy\Default.rdp'
$LinesCount = $LinesArray.Count
for ($i=0; $i -lt $LinesCount; $i++) {
    foreach ($OldString in $OldStrings) {
        foreach ($NewString in $NewStrings) {
            if ($LinesArray[$i] -like $OldString) {
                $LinesArray[$i] = $LinesArray[$i] -replace $OldString, $NewString
                Write-Host "`nline" $i "takes on value:" $LinesArray[$i] "`n" -ForegroundColor Gray
            }
        }
    }
}

该文件可能就是为什么它根本不被阅读。

执行脚本后,我只看到

line 2 takes on value: desktopwidth:i:1734 

line 3 takes on value: desktopwidth:i:1734 

line 5 takes on value: desktopwidth:i:1734

2 个答案:

答案 0 :(得分:0)

您需要两次查看字符串数组。您想要执行两个循环,一个用于文件中的每一行,另一个用于您要替换的行中的每个计数。我认为这应该有效:

$OldStrings = @(
"desktopwidth:i:1440",
"desktopheight:i:900",
"winposstr:s:0,1,140,60,1596,999"
)
$NewStrings = @(
"desktopwidth:i:1734",
"desktopheight:i:990",
"winposstr:s:0,1,50,7,1800,1036"
)

$LinesArray = Get-Content -Path 'C:\temp\My Copy\Default.rdp'

# loop through each line
for ($i=0; $i -lt $LinesArray.Count; $i++)
{
    for ($j=0;$j -lt $OldStrings.Count; $j++)
    {
        if ($LinesArray[$i] -match $OldStrings[$j])
        {
            $LinesArray[$i] = $LinesArray[$i] -replace $OldStrings[$j],$NewStrings[$j]
            Write-Host "`nline" $i "takes on value:" $LinesArray[$i] "`n" -ForegroundColor Gray
        }
    }
}

$LinesArray | Set-Content -Path 'C:\temp\My Copy\Default.rdp'

答案 1 :(得分:0)

您不需要费心检查线路以查找匹配项。既然您已准备好更换件,那么无论如何都要直接进行更换。这种方式也应该更快。

String query="insert into exam(sub_id,exam_sem,co,tch_id,ex_pwd,ex_date,ex_duration,noofq,Desc) values(?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt=DatabaseConnector.getPreparedStatement(query);
//pstmt.setInt(1,ed.getEx_id());
pstmt.setInt(1,ed.getSub_id());
pstmt.setString(2,ed.getEx_sem());
pstmt.setString(3,ed.getEx_co());
pstmt.setInt(4,ed.getT_id());
pstmt.setString(5,ed.getEx_pwd());
pstmt.setString(6,ed.getEx_date());
pstmt.setString(7,ed.getEx_duration());
pstmt.setInt(8,ed.getNoofq());
pstmt.setString(9,ed.getDesc());

$stringReplacements = @{ "desktopwidth:i:1440" = "desktopwidth:i:1734" "desktopheight:i:900" = "desktopheight:i:990" "winposstr:s:0,1,140,60,1596,999" = "winposstr:s:0,1,50,7,1800,1036" } $path = 'C:\temp\My Copy\Default.rdp' # Read the file in as a single string. $fileContent = Get-Content $path | Out-String # Iterate over each key value pair $stringReplacements.Keys | ForEach-Object{ # Attempt the replacement for each key/pair search/replace pair $fileContent =$fileContent.Replace($_,$stringReplacements[$_]) } # Write changes back to file. # $fileContent | Set-Content $path 是搜索和替换字符串的键值散列。我没有看到你把更改写回文件,所以我在最后留下了一行让你取消注释。

如果您重视$stringReplacements行,您可以添加检查以进行替换,但我认为这是用于调试的,并且您已经知道如何执行此操作。