4

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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jerry Sweeton
  • 163
  • 2
  • 4
  • 15

2 Answers2

3

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"
zdan
  • 28,667
  • 7
  • 60
  • 71
2

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
}
mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • 1
    Would it be too much to ask for you to have a statement like "in your replacement text, use the backtick `\`` instead of a backslash ``\``, like below:" (?) – Code Jockey Jun 11 '15 at 17:47