我正在尝试解析文本文档中的模式,目的是消除该模式及其前一行。此命令行向我确认我正在选择所需的数据:
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的新手,非常感谢任何正确方向的指点。谢谢您抽时间见我!
原始数据:
A0000 FMC 18001 0000 18009 0820 200.3
A0000传输18001 0000 00000 0000 744.0
*前一状态为不可上报*
A0000 FMC 18031 1600 00000 00008.0
A0000 PMCM 18031 1200 18031 1600 4.0
A0000 FMC 18017 1303 18031 1200 334.9
A0000 NMCM 18017 0700 18017 1303 6.1
A0000 FMC 18001 0000 18017 0700 391.0
A0000传输18001 0000 00000 0000 744.0
*前一状态为不可上报*
A0000 FMC 18017 2200 00000 0000 338.0
A0000 PMCM 18017 1410 18017 2200 7.8
修改后的数据:
A0000 FMC 18001 0000 18009 0820 200.3
A0000 FMC 18031 1600 00000 00008.0
A0000 PMCM 18031 1200 18031 1600 4.0
A0000 FMC 18017 1303 18031 1200 334.9
A0000 NMCM 18017 0700 18017 1303 6.1
A0000 FMC 18001 0000 18017 0700 391.0
A0000 FMC 18017 2200 00000 0000 338.0
A0000 PMCM 18017 1410 18017 2200 7.8
发布于 2018-03-03 00:48:16
这可能不是最好的方法(从长远来看),但您可以尝试这样做:
$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 } |% {
$_
}https://stackoverflow.com/questions/49061507
复制相似问题