繁体   English   中英

Powershell选择字符串列出多个匹配项

[英]Powershell Select-String list multiple matches

我正在尝试根据一些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
$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM