我正在使用powershell,并想根据匹配项删除一行文本,然后在具有匹配项的行之后删除一定数量的行。我有以下代码,它将删除具有匹配项的当前行。如何删除下一行?
获取内容$ filePath | ?{$ _ -notmatch“`f”} | 设置内容$ outPath
即使更长,但如果您有更复杂的“规则”来确定要删除的行,它可能是有用的基础。(尽管我更喜欢@mjolinor的答案)
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[String]$Source,
[Parameter(Mandatory=$true)]
[String]$Target,
[Parameter(Mandatory=$true)]
[Int32]$Lines
)
$LinesLeftToRemove = 0
$TargetContent = @()
(Get-Content $Source) | % {
If($_ -notmatch "`f") {
$LinesLeftToRemove = $Lines + 1
}
If($LinesLeftToRemove -GT 0) {
$LinesLeftToRemove--
} Else {
$TargetContent += $_
}
}
Set-Content $Target $TargetContent
请注意,这应该放在SomeScript.ps1
文件中并按以下方式使用:
.\SomeScript.ps1 -Source .\Source.txt -Target .\Target.txt -Lines 5
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句