我想在Powershell中将内容添加到文本文件的中间.我正在搜索特定模式,然后在其后添加内容.请注意,这是在文件的中间.
我现在拥有的是:
(Get-Content ( $fileName )) |
Foreach-Object {
if($_ -match "pattern")
{
#Add Lines after the selected pattern
$_ += "`nText To Add"
}
}
} | Set-Content( $fileName )
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.我假设因为$ _是不可变的,或者因为+ =运算符没有正确修改它?
将文本附加到$ _的方法是什么,这将反映在以下Set-Content调用中?
powershell ×1