【发布时间】:2016-10-27 15:52:03
【问题描述】:
我有生成的文本文件,每个文本块之间有 2 个空行。我可以使用 Notepad++ 将 \r\n\r\n 替换为 \r\n 来执行此操作,但必须有一种方法可以自动执行此操作。
我尝试在 Powershell 中提出一些建议,但到目前为止没有任何效果。
这是我迄今为止尝试过的:
(Get-Content .\test.txt).Replace("\n\n",'\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\s+\r\n+",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\r\n+",'') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\n+",'') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\r\n\r\n",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("^(\s+\r\n)",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("^(\s+\r\n+)",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("^(\r\n+)",'\r\n') | Set-Content .\test.txt
(Get-Content .\test.txt).Replace("\r\n",'\b') | Set-Content .\test.txt
【问题讨论】:
-
Test.txt的内容是什么你也许可以使用Trim()方法删除空格。 -
使用`代替\来转义控制字符
-
@Ramil 如果您发现任何答案有帮助,请接受。
-
抱歉,忘记添加内容了。我目前正在处理一个看起来像这样的测试文件。 text
下一个文本块(对不起,我不知道如何在 cmets 中添加空行,但你应该明白我的意思:P)
标签: powershell replace