2013-03-07 301 views
4

是否有方法让PowerShell查找并替换字符串(例如〜}} | {{E)带回车和换行符(\ r \ nE)?在PowerShell中查找/替换带回车符和换行符的字符串

例如:

$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

如何:

$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

那就太过分的要求你有一个像声明“在您的替换文本,使用反引号'\''而不是一个反斜杠''\'',象下面这样:” (?) – 2015-06-11 17:47:45

2

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

"`r`nE"