Find/replace string with carriage return and line feed in PowerShell

冷暖自知 提交于 2020-01-03 17:08:22

问题


Is there a way to have PowerShell find and replace a string (for example ~}}|{{E) with a carriage return and line feed (\r\nE)?

For example:

$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
}
|improve this question

回答1:


How about this:

$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:


To create a string that contains the control characters with carriage return and line feed, you would use double quotes and use the backtick character ` which is the powershell escape code. Like this:

"`r`nE"


来源:https://stackoverflow.com/questions/15283931/find-replace-string-with-carriage-return-and-line-feed-in-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!