Reputation: 4681
To find a simple pattern in a set of files in powershell I go
$pattern= 'mypattern'
$r= Get-ChildItem -Path "C:\.." -recurse |
Select-String -pattern $pattern | group path | select name
$r | Out-GridView
In my scenario, I have files that contain the pattern for more than one time and others that have the pattern for one time only. So I am interested in those files that contain the pattern for more than one time and not interested in the rest. Thanks
Upvotes: 1
Views: 1952
Reputation: 46730
One approach for the start of what you are looking for is Select-String
and Group-Object
like you already have.
Select-String -Path (Get-ChildItem C:\temp\ -Filter *.txt -Recurse) -Pattern "140" -AllMatches |
Group-Object Path |
Where-Object{$_.Count -gt 1} |
Select Name, Count |
Out-GridView
This will take all the txt files in the temp directory and group them by the number of matches. -AllMatches
is important as by default Select-String
will only return the first match it finds on a line.
Of those groups we take the ones where the count is higher than one using Where-Object
. Then we just output the file names and there counts with a Select Name,Count
. Where name
is the full file path where the matched text is located.
Out-GridView
I see that you are assinging the output from Out-GridView
to $r
. If you want to do that you need to be sure you add the -PassThru
parameter.
Upvotes: 1