Malachy
Malachy

Reputation: 392

Search array of files using Select-String

I am able to use Select-String to return a list of files that contain some text that i am looking for:

Get-ChildItem *.cs -recurse | Select-String -SimpleMatch ".ToList()" | group path | select name

What I am trying to do is apply subsequent filters to the result of my query. I would like to do something like:

if($fileSet -eq $null)
{
    $fileSet = Get-ChildItem *.cs -recurse | Select-String -SimpleMatch ".ToList()" | group path | select name 
}

$fileSet | Select-String -SimpleMatch "Additional Term" | group path | select name

I think i need a way to pipe the contents of an array of files ($fileSet) to Select-String; however I'm pretty sure I going about this all wrong. Any help appreciated!

Upvotes: 1

Views: 389

Answers (2)

Malachy
Malachy

Reputation: 392

Was able to massage the accepted answer to also maintain something similar to structure in the question

if($fileSet -eq $null)
{
    $fileSet = Get-ChildItem *.cs -recurse | Select-String -SimpleMatch ".ToList()"
}

$fileSet | foreach {
   Select-String -Path $_.Path -SimpleMatch "Additional Term"
} | Group-Object Path | Select-Object -ExpandProperty Name

Upvotes: 0

Bill_Stewart
Bill_Stewart

Reputation: 24525

How about something like:

Get-ChildItem *.cs -Recurse | Select-String -SimpleMatch ".ToList()" | ForEach-Object {
  $_ | Select-String -SimpleMatch "Additional Term"
} | Group-Object Path | Select-Object -ExpandProperty Name

Upvotes: 2

Related Questions