我对脚本很陌生(几周),很高兴得到你的帮助。我有一个需要更改的日志文件 (.txt)。
内容总是一样的:
random text
random text
successfull
error
random text
random text
random text
error
...
我想删除包含单词“error”的行,但前提是上面的行包含单词“successfull”。
到目前为止,我设法从文件中获取了所有匹配的字符串并能够替换它们,但是在此过程中我丢失了其余的文本:
get-content "D:\test.txt" | select-string -pattern "error" -context 1,0 | Where-Object {"$_" -match "successfull" } | %{$_ -replace "error.*"} | Out-File "D:\result.txt"
我真的很感谢你在这里的帮助。