Set-Content 在我的文件末尾附加一个换行符(换行符,CRLF) [英] Set-Content appends a newline (line break, CRLF) at the end of my file
问题描述
我的原始配置文件 (web1.config) 没有多余的行,在记事本中查看时(显示所有字符)如下所示:
My original config file (web1.config) has no extra line and when viewed in notepad (showing all characters) looks as:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6" />
<httpRuntime targetFramework="4.6" />
</system.web>
<appSettings>
<add key="myConnectionString" value="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</appSettings>
</configuration>
现在,我需要应用脚本将我的数据库名称更改为其他类似的名称:
Now, I need to apply the script to change my database name to something else which looks like:
Move-Item "web1.config" "webtemp.config"
Get-Content "webtemp.config" | ForEach-Object {$_ -replace "database=myDb;", "database=newDb;"} |Set-Content "web1.config" -Force
Remove-Item "webtemp.config"
Write-Output('Settings Changed')
因此,生成的新文件 (web1.config) 如下所示:
So, the new file (web1.config) generated looks as:
注意文件末尾添加的额外行(完全不需要)我尝试了所有其他选项,例如:- 使用输出文件 api- 使用 .net IO 方法 System.IO.StreamWriter- 使用 -nonewline 标志(它将所有 10 行转换为单行)- 使用不同的编码选项- 尝试将 替换为 (不要再次工作,因为 set-content 总是会生成 crlf)
Notice the extra line added at the end of the file (which is completely not needed) I tried all other options such as: - using out-file api - using .net IO method System.IO.StreamWriter - using -nonewline flag (it converts all 10 lines into single line) - using different encoding options - tried replacing to (don't work as again set-content generates the crlf always)
我使用的是 PowerShell v5.1.
I'm using PowerShell v5.1.
推荐答案
tl;dr(PSv5+;旧版本见底部):
(Get-Content webtemp.config) -replace 'database=myDb;', 'database=newDb;' -join "`n" |
Set-Content -NoNewline -Force web1.config
注意:替换`n";
与 "`r`n";
如果你想要 Windows 风格的 CRLF 行结尾而不是 Unix 风格的 LF-only 行结尾(PowerShell 和许多实用程序都可以处理).
Note: Replace "`n"
with "`r`n"
if you want Windows-style CRLF line endings rather than Unix-style LF-only line endings (PowerShell and many utilities can handle both).
在PSv5+中,Set-Content
支持-NoNewline
开关,它指示Set-Content
不要在每个输入对象后添加换行符(换行符).这同样适用于 Add-Content
和 Out-File
cmdlet.
In PSv5+, Set-Content
supports the -NoNewline
switch, which instructs Set-Content
not to add a newline (line break) after each input object. The same applies analogously to the Add-Content
and Out-File
cmdlets.
换句话说:Set-Content -NoNewline
直接连接所有输入对象的字符串表示:
In other words: Set-Content -NoNewline
directly concatenates the string representations of all its input objects:
PS> 'one', 'two' | Set-Content -NoNewline tmp.txt; Get-Content tmp.txt
onetwo
如果你传递给 Set-Content -NoNewline
的是一个 单个字符串,它已经嵌入了换行符,你可以使用它按原样获得所需的结果:
If what you're passing to Set-Content -NoNewline
is a single string that already has embedded newlines, you can use it as-is and get the desired result:
PS> "one`ntwo" | Set-Content -NoNewline tmp.txt; "$(Get-Content -Raw tmp.txt)?"
one
two?
请注意,Get-Content -Raw
将文件作为一个整体读取,原样(除了字符解码)以及 ?
直接出现在 two
之后表示文件没有尾随换行符.
Note that Get-Content -Raw
reads the file as a whole, as-is (aside from character decoding) and the fact that the ?
appears directly after two
implies that the file has no trailing newline.
就您而言,由于您正在一个接一个处理输入行(通过 Get-Content
without -Raw
) 并因此输出一个 array 行(字符串),您必须首先用换行符将它们连接起来作为 分隔符 - 仅在行之间 - 并将结果传递给 Set-Content -NoNewline
,如顶部所示;这是一个简化的例子:
In your case, since you're processing input lines one by one (via Get-Content
without -Raw
) and therefore outputting an array of lines (strings), you must first join them with a newline as the separator - between lines only - and pass the result to Set-Content -NoNewline
, as shown at the top; here's a simplified example:
PS> ('one', 'two') -join "`n" | Set-Content -NoNewline tmp.txt; "$(Get-Content -Raw tmp.txt)?"
one
two?
'one', 'two'
是一个双元素字符串数组,它是您的逐行处理命令的替代品.
'one', 'two'
is a two-element string array that is a stand-in for your line-by-line processing command.
编码说明:
在 Windows PowerShell 中,Set-Content
默认情况下会根据系统的旧单字节代码页生成ANSI"编码的文件.
要明确控制编码,请使用 -Encoding
参数.
In Windows PowerShell, Set-Content
produces "ANSI"-encoded files by default, based on your system's legacy, single-byte code page.
To control the encoding explicitly, use the -Encoding
parameter.
在 PSv4- 中,需要使用 .NET Framework 的解决方案:
In PSv4-, a solution that uses the .NET Framework is needed:
PS> [System.IO.File]::WriteAllText('tmp.txt', ('one', 'two') -join "`n"); "$(Get-Content -Raw tmp.txt)?"
one
two?
请注意,[System.IO.File]::WriteAllText()
,在没有编码参数的情况下,默认为BOM-less UTF-8.
传递所需的 [System.Text.Encoding]
编码实例作为需要的第三个参数.
Note that [System.IO.File]::WriteAllText()
, in the absence of an encoding argument, defaults to BOM-less UTF-8.
Pass the desired [System.Text.Encoding]
encoding instance as the 3rd argument as needed.
这篇关于Set-Content 在我的文件末尾附加一个换行符(换行符,CRLF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!