在保存为UTF8时,如何在保留现有换行符的同时防止设置内容的换行符? [英] How can I prevent additional newlines with set-content while keeping existing ones when saving in UTF8?
问题描述
我有一个小的powershell脚本,该脚本读取UTF8编码的文档,对其进行替换,然后将其保存起来,如下所示:
I have a small powershell script which reads a document with UTF8 encoding, makes some replacements in it and saves it back which looks like this:
(Get-Content $path) -Replace "myregex","replacement" | Set-Content $path2 -Encoding utf8
这将创建一个具有正确编码和正确内容的新文件,但末尾还有其他换行符.根据此答案和许多其他答案,我被告知要么:
This will create a new file with the right encoding and right contents but there are additional new line characters at the end. According to this answer and many others, I am told to either:
- 将参数
-NoNewLine
添加到Set-Content
- 使用
[System.IO.File]::WriteAllText($path2,$content,[System.Text.Encoding]::UTF8)
- Add the parameter
-NoNewLine
toSet-Content
- Use
[System.IO.File]::WriteAllText($path2,$content,[System.Text.Encoding]::UTF8)
这两种解决方案都删除尾随的新行... 以及文件中的所有其他新行.
Both solutions remove the trailing new lines... and every other new lines in the file.
是否有两种方式 :
- 在保存文件时删除尾随的新行.
- 保留文件中现有的新行.
推荐答案
[IO.File]::WriteAllText()
假定$content
是单个字符串,但是Get-Content
产生一个字符串数组(并从每个字符串的末尾删除换行符行/字符串).将字符串数组改成单个字符串会使用$OFS
字符将字符串连接起来(请参见此处).
[IO.File]::WriteAllText()
assumes that $content
is a single string, but Get-Content
produces an array of strings (and removes the line breaks from the end of each line/string). Mangling that string array into a single string joins the strings using the $OFS
character (see here).
要避免此行为,您需要确保在将$content
传递给WriteAllText()
时,它已经是单个字符串.有多种方法可以做到这一点,例如:
To avoid this behavior you need to ensure that $content
already is a single string when it's passed to WriteAllText()
. There are various ways to do that, for instance:
-
使用
Get-Content -Raw
(PowerShell v3或更高版本):
Use
Get-Content -Raw
(PowerShell v3 or newer):
$content = (Get-Content $path -Raw) -replace 'myregex', 'replacement'
通过Out-String
将输出管道:
Pipe the output through Out-String
:
$content = (Get-Content $path | Out-String) -replace 'myregex', 'replacement' -replace '\r\n$'
但是请注意,正如注释中指出的那样,Out-String
(就像Set-Content
一样)会添加尾随换行符.您需要通过第二次替换操作将其删除.
Note, however, that Out-String
(just like Set-Content
) adds a trailing line break, as was pointed out in the comments. You need to remove that with a second replacement operation.
使用-join
运算符加入数组:
$content = (Get-Content $path) -replace 'myregex', 'replacement' -join "`r`n"
这篇关于在保存为UTF8时,如何在保留现有换行符的同时防止设置内容的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!