我试图解析一个模式的文本文档,目的是消除模式及其前一行。这个命令行向我确认我正在选择所需的数据:
Get-Content -Path '.\460-est-5.txt' | Select-String -Pattern "THE PREVIOUS" SimpleMatch -AllMatches -Context 1,0
由于我希望消除此问题并重定向输出,因此我生成此命令,并且它不会查询任何内容:
Get-Content -Path '.\460-est-5.txt' | Select-String -Pattern "THE PREVIOUS" -SimpleMatch -NotMatch -AllMatches -Context 1,0
然而,当我消除-Context 1,0
时,它几乎查询了我需要的所有数据,显然在我想要之前留在行中。
-Context 1,0
一直在击败-NotMatch
,我已经挖掘了帮助文件,MSDN,StackOverflow,我正在看看我是否可以创建一个脚本,但这无关紧要因为我需要两个参数-NotMatch
和-Context
(至少据我所知!)。
我为缺乏知识而道歉。我是PowerShell的新手,并欣赏任何指向正确的方向。感谢您的时间!
原始数据:
修改数据:
这可能不是最好的方法(通过远景),但你可以试试这个:
$filePath = ".\460-est-5.txt"
$query = "THE PREVIOUS"
& {
$currentLine = $null
$lastLine = $null
Get-Content -Path $filePath |% {
$lastLine = $currentLine
$currentLine = $_
if ($currentLine -eq $query) {
$currentLine = $null
return
}
return $lastLine
}
if ($currentLine) {
return $currentLine
}
} |? { $_ -ne $null } |% {
$_
}