【发布时间】:2021-10-24 23:40:45
【问题描述】:
环境:Windows 10 pro 20H2,PowerShell 5.1.19041.1237
在.txt 文件中,我的以下PowerShell 代码没有用" " 替换换行符。 问题:我可能在这里遗漏了什么,我们怎样才能让它发挥作用?
C:\MyFolder\Test.txt 文件:
This is first line.
This is second line.
This is third line.
This is fourth line.
所需的输出 [用“”字符替换换行符后]:
This is first line. This is second line. This is third line. This is fourth line.
PowerShell 代码:
PS C:\MyFolder\test.txt> $content = get-content "Test.txt"
PS C:\MyFolder\test.txt> $content = $content.replace("`r`n", " ")
PS C:\MyFolder\test.txt> $content | out-file "Test.txt"
备注
如果我替换文件中的一些其他字符,上面的代码可以正常工作。比如我把上面代码的第二行改成$content = $content.replace("third", "3rd"),代码就成功的把上面文件中的third替换成了3rd。
【问题讨论】:
-
注意out-file默认使用utf16编码。