在保存为UTF8时,如何在保留现有换行符的同时防止设置内容的换行符? [英] How can I prevent additional newlines with set-content while keeping existing ones when saving in UTF8?

查看:70
本文介绍了在保存为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:

  1. 将参数-NoNewLine添加到Set-Content
  2. 使用[System.IO.File]::WriteAllText($path2,$content,[System.Text.Encoding]::UTF8)
  1. Add the parameter -NoNewLine to Set-Content
  2. 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.

是否有两种方式 :

  1. 在保存文件时删除尾随的新行.
  2. 保留文件中现有的新行.

推荐答案

[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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆