PowerShell - 在另一个字符串之后的下一行在文本文件中插入字符串

Con*_*989 5 powershell

这似乎是一个简单的,但我就是无法围绕它/找到一篇涵盖它的帖子

我正在尝试使用 PowerShell 修改文本(配置)文件查找特定字符串 (A) 出现的位置,然后将另一个字符串 (B) 添加到下一行。保留字符串 (A) 出现的行

所以问题是我不能做一个简单的查找和替换,因为出现字符串 (A) 的行后面有其他文本

这是为了希望有人比我知道的更聪明。干杯

Dav*_*ant 4

# Let's say my file test.txt contains
# Line1
# Line2
# Line3
# Line4

$lines = Get-Content test.txt
$pos = [array]::indexof($lines, $lines -match "Line3") # Could use a regex here
$newLines = $lines[0..($pos -1)], "MyNewLine3", $lines[$pos..($lines.Length - 1)]

$newLines | Set-Content test.txt
Run Code Online (Sandbox Code Playgroud)