ciso
ciso

Reputation: 3050

How to replace a backslash CR LF with a <br> with powershell

I've tried many combinations of attempting to replace a "\ cr lf" with an html "br" tag after "foo".

My input file (tempA.txt) looks like this (there is a cr lf at the end of line 1 after the slash):

foo\
bar

I'm using a powershell command (within a bat file) like this:

type c:\temp\tempA.txt
powershell -Command "(gc c:\temp\tempA.txt) -replace '\\`r`n', '<br>' | Out-File c:\temp\tempB.txt"
type c:\temp\tempB.txt

My output file (tempB.txt) isn't changing. I want the output file to contain

foo<br>bar

How do you replace the "\ cr lf" with a simple html "br" tag?

Upvotes: 1

Views: 1887

Answers (2)

Jan Chrbolka
Jan Chrbolka

Reputation: 4464

Get-Content will split your file into an array of lines. Based on this, you can just replace \ with <br> and join the lines together.

(gc c:\temp\tempA.txt) -replace '\\', '<br>' -join '' | Out-File c:\temp\tempB.txt

Upvotes: 1

Keith Hill
Keith Hill

Reputation: 202052

Change your first -replace operator arg to use a double quoted string:

... -replace "\\`r`n", '<br>'

A single quoted string in PowerShell is a literal string so the backtick doesn't work to escape characters.

Upvotes: 2

Related Questions