【问题标题】:How to Select-String from Multiple Lines with Powershell如何使用 Powershell 从多行中选择字符串
【发布时间】:2021-07-12 23:45:44
【问题描述】:

我在 test.dat 下面有这个文件

        <category>Games</category>
</game>

        <category>Applications</category>
</game>

        <category>Demos</category>
</game>

        <category>Games</category>
        <description>MLB 2002 (USA)</description>
</game>

        <category>Bonus Discs</category>
</game>

        <category>Multimedia</category>
</game>

        <category>Add-Ons</category>
</game>

        <category>Educational</category>
</game>

        <category>Coverdiscs</category>
</game>

        <category>Video</category>
</game>

        <category>Audio</category>
</game>

        <category>Games</category>
</game>

如何使用Get-Content 和Select-String 从上述文件的输入将以下内容输出到终端。使用上面的输入,我需要接收这个输出。

            <category>Games</category>
    </game>
            <category>Games</category>
    </game>

这是我当前正在使用的命令,但它不起作用。 Get-Content '.\test.dat' | Select-String -pattern '(^\s+&lt;category&gt;Games&lt;\/category&gt;\n^\s+&lt;\/game&gt;$)'

【问题讨论】:

  • 看起来像 xml 那么为什么不作为 xml 处理呢?你打算用不匹配过滤掉什么?您实际上想要达到什么目的?请提供预期结果。
  • 它是 xml 但这是我熟悉的唯一方式。基本上我只需要去掉 Games
  • 去掉它并留下文件的其余部分?将其剥离并完全按照您在那里展示的方式存储?你想究竟得到什么?请编辑您的帖子以提供这些重要的详细信息。
  • 我已经为你简化了。

标签: regex powershell text select-string


【解决方案1】:

首先,您需要将其作为一个字符串全部读取以跨行匹配。

Get-Content '.\test.dat' -Raw

由于您似乎想排除条目,因此您可以使用此模式仅抓取前后没有空格的条目

'(?s)\s+<category>Games\S+\r?\n</game>'

选择字符串返回一个matchinfo对象,你需要提取Matches属性的Value属性。您可以通过几种不同的方式做到这一点。

Get-Content '.\test.dat' -Raw |
    Select-String '(?s)\s+<category>Games\S+\r?\n</game>' -AllMatches |
        ForEach-Object Matches | ForEach-Object Value

或

$output = Get-Content '.\test.dat' -Raw |
    Select-String '(?s)\s+<category>Games\S+\r?\n</game>' -AllMatches

$output.Matches.Value

或

(Get-Content '.\test.dat' -Raw |
    Select-String '(?s)\s+<category>Games\S+\r?\n</game>' -AllMatches).Matches.Value

输出

        <category>Games</category>
</game>


        <category>Games</category>
</game>

您也可以使用[regex] 类型的加速器。

$str = Get-Content '.\test.dat' -Raw

[regex]::Matches($str,'(?s)\s+<category>Games\S+\r?\n</game>').value

编辑

根据您的附加信息,我的理解是您想要删除所有空的游戏类别。我们可以通过使用 here 字符串大大简化这一点。

$pattern = @'
        <category>Games</category>
    </game>

'@

额外的空行是为了捕获最后的换行符。你也可以这样写

$pattern = @'
        <category>Games</category>
    </game>\r?\n
'@

现在,如果我们对模式进行替换,您将看到我认为您对最终结果的期望。

(Get-Content $inputfile -Raw) -replace $pattern

要完成它,您只需将上述命令放在Set-Content 命令中即可。由于Get-Content命令用括号括起来,所以在写入文件之前,它会被完全读入内存。

Set-Content -Path $inputfile -Value ((Get-Content $inputfile -Raw) -replace $pattern)

编辑 2

嗯,它似乎在 ISE 中有效,但在 powershell 控制台中无效。如果你遇到同样的事情,试试这个。

$pattern = '(?s)\s+<category>Games</category>\r?\n\s+</game>'

Set-Content -Path $inputfile -Value ((Get-Content $inputfile -Raw) -replace $pattern)

【讨论】:

  • 所以这最终比我想象的要复杂得多。这适用于测试文件,但我想要的真实文件不是。由于文件非常大,我创建了一个 pastebin。但这是我正在使用的数据pastebin.com/q33wFdAX。自动化从文件中删除这些双行的最佳方法是什么?
  • 基本上我想使用 -notmatch 来获取除 Games 之外的所有内容,因此我可以编写一个没有这些行的新文件。这在记事本++中真的很容易,但我正在寻找一种自动化的方法。
  • 但是你真的想离开 MLB 的游戏部分吗?
  • 是的,包含我想要保留的数据的游戏部分。如果它是空的,那么没有
  • 只需在末尾多加一个\r?\n
猜你喜欢
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多