2015-07-22 64 views
1

我尝试了很多尝试用“html”后面的“foo”标签替换“\ crf”的组合。如何用powershell替换反斜杠CR LF

我的输入文件(tempA.txt)看起来像这样(有1行的末尾CR LF斜线后):

foo\ 
bar 

我使用的是蝙蝠中的PowerShell命令(文件)像这样:

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 

我的输出文件(tempB.txt)没有改变。我希望输出文件包含

foo<br>bar 

如何用简单的html“br”标记替换“\ cr lf”?

回答

1

Get-Content会将您的文件拆分为一行数组。基于此,您可以用<br>替换\,然后将这些行结合在一起。

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

改变你的第一-replace运营商ARG使用双引号的字符串:

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

PowerShell中的一个单引号字符是一个字符串,因此反引号不工作逃脱字符。