1

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

1 Answer 1

1
$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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.