Powershell如何在某些字符串后添加新行

Pav*_*vak 1 powershell newline

我是脚本新手.我试图解决一个问题,我正在看windows批处理和PowerShell.我找到了这个,但我不知道,为什么它不起作用:

我有input.txt文件包含这个内容:

<a> TEXT </a>
<c> text asdrtlj </c>
<a> another text </a>
Run Code Online (Sandbox Code Playgroud)

我想在每个之前添加新行<a>,所以我尝试了这个:

powershell -Command "(gc input.txt) -replace '<a>', '`r`n<a>' | Out-File output.txt"
Run Code Online (Sandbox Code Playgroud)

添加换行符不起作用.你能帮我吗?

PS:我在搜索解决方案时发现了很多复杂的代码,我还没有理解它们,所以如果你推荐我一些很好的教程,从这些语言开始,我会很感激.

Mar*_*agg 5

这里有几个问题.通过-commandpowershell开关执行命令会导致一些由<>字符引起的保留字符问题.

而是打开PowerShell.exe窗口(开始 - >运行 - > powershell)并直接执行命令.

您还需要使用双引号而不是单引号,以便使用`r和`n等特殊代码.也应该足够了:

(gc input.txt) -replace '<a>', "`n<a>" | Out-File output.txt
Run Code Online (Sandbox Code Playgroud)