2013-03-07 300 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"