简体   繁体   中英

Powershell Select-String list multiple matches

I'm trying to do some pattern-matching based a list of words against some XML files.

I have the following:

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

My problem appears when there are multiple matches on the same line of code,

eg if the $WordList = "AA","AACC", and the line is:

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

The $Result would be only a single-line match based on the first word (AA). But it won't give me all three results, one for the both matches based on "AA" and another one for the match on "AACC", all on the same line.


Current $Result:

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

Ideal $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 

Output:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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