Powershell Select-String列出多个匹配项

时间:2017-10-10 15:57:58

标签: regex powershell select-string

我尝试根据某些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"和#34; AACC"上的另一个,都在同一条线上。

当前$结果:

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

理想的$结果:

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

1 个答案:

答案 0 :(得分:0)

$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
相关问题