【问题标题】:Powershell Select-String list multiple matchesPowershell Select-String 列出多个匹配项
【发布时间】:2023-03-23 10:23:01
【问题描述】:

我正在尝试根据一些 XML 文件的单词列表进行一些模式匹配。

我有以下几点:

$Result = Get-ChildItem -Recurse $FullPath\*.xml |
  Select-String -Pattern $WordList |
    Select-Object Path, Pattern, Line, LineNumber

我的问题出现在同一行代码有多个匹配项时,

例如,如果 $WordList = "AA","AACC",则该行是:

"This is a test line AA, BB, AACC, KK"

$Result 将仅是基于第一个单词 (AA) 的单行匹配。但它不会给我所有三个结果,一个是基于“AA”的两个匹配,另一个是基于“AACC”的匹配,都在同一行。


当前 $Result:

Path Pattern Line LineNumber
**   AA      This is a test line AA, BB, AACC, KK 12

理想的 $Result:

Path Pattern Line LineNumber
**   AA      This is a test line AA, BB, AACC, KK 12
**   AA      This is a test line AA, BB, AACC, KK 12
**   AACC    This is a test line AA, AABB, AACC, KK 12

【问题讨论】:

    标签: regex powershell select-string


    【解决方案1】:
    $WordList = "AA","AACC"
    $Result = $WordList | Foreach { 
        Get-ChildItem .\Test\*.txt |
            Select-String -Pattern $_ } |
                Select-Object Path, Pattern, Line, LineNumber 
    

    输出:

    $Result
    
    Path Pattern Line                            LineNumber
    ---- ------- ----                            ----------
    **   This is a test line AA, BB, AACC, KK 12 1
    **   This is a test line AA, BB, AACC, KK 12 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 2023-02-25
      • 1970-01-01
      • 2018-09-07
      • 2017-12-16
      • 2014-12-16
      • 2022-11-29
      相关资源
      最近更新 更多