[英]Powershell append string to text creates a new line
Im trying to create a ps script which appends the string ;history directly after ;stylesheets but currently the script is creating an additional line when i want it to append it directly after. 我试图创建一个ps脚本,它附加字符串;历史记录直接;样式表但当前脚本正在创建一个额外的行,当我希望它直接追加它。
This is how it is suppose to look 这就是它的设想
;stylesheets;history
But this is what happens 但这就是发生的事情
;stylesheets
;history
Can someone explain why? 有人可以解释原因吗?
(Get-Content K:\Temp\test.properties) |
Foreach-Object {
$_
if ($_ -match ";stylesheets")
{ ";history"
}
} | Set-Content K:\Temp\test.properties
You are getting two lines because you are returning two objects when you match. 你得到两行,因为你匹配时返回两个对象。
@($_,";history")
and each object is written as a line. @($_,";history")
并将每个对象写成一行。 You could combine them into one string when you match. 匹配时,您可以将它们组合成一个字符串。
(Get-Content K:\Temp\test.properties) |
Foreach-Object {
if ($_ -match ";stylesheets") {
"$_;history"
} else {
$_
}
} | Set-Content K:\Temp\test.properties
Alternative solution: 替代方案:
(Get-Content K:\Temp\test.properties).Replace(';stylesheets',';stylesheets;history') |
Set-Content K:\Temp\test.properties
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.