powershell Select-String查询

问题描述 投票:1回答:1

我试图解析一个模式的文本文档,目的是消除模式及其前一行。这个命令行向我确认我正在选择所需的数据:

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 TRAN 18001 0000 00000 0000 744.0

*****以前的状态是不可报告的*****

A0000 FMC 18031 1600 00000 0000 8.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 TRAN 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 0000 8.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

windows powershell cmd
1个回答
0
投票

这可能不是最好的方法(通过远景),但你可以试试这个:

$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 } |% {
    $_
}
© www.soinside.com 2019 - 2024. All rights reserved.