在PowerShell中使用回车符和换行符查找/替换字符串

时间:2013-03-07 23:30:05

标签: powershell control-characters

有没有办法让PowerShell使用回车符和换行符找到并替换字符串(例如〜}} | {{E )( \ r \ nE )?

例如:

$filenames = @("E:\blergfest.csv")
foreach ($file in $filenames) {
    $outfile = "$file" + ".out"

    Get-Content $file | Foreach-object {
        $_ -replace '\~}}|{{E', '\r\nE' `
            -replace '\|\r\n', '\r\n'
    } | Set-Content $outfile
}

2 个答案:

答案 0 :(得分:2)

这个怎么样:

$filenames = @("E:\blergfest.csv")
foreach ($file in $filenames) {
    $outfile = "$file" + ".out"

    Get-Content $file | Foreach-object {
        $_ -replace '\~}}|{{E', "`r`nE" `
            -replace '\|\r\n', "`r`n"
    } | Set-Content $outfile
}

答案 1 :(得分:2)

要创建一个包含带回车符和换行符的控制字符的字符串,您可以使用双引号并使用反引号字符`这是powershell转义码。像这样:

"`r`nE"