JohD
JohD

Reputation: 25

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,

e.g 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

Upvotes: 1

Views: 3192

Answers (1)

Vincent K
Vincent K

Reputation: 1346

$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

Upvotes: 1

Related Questions