Reputation: 39
I have lets say 8 files named log1.txt, log2.txt, log3.txt ... saved in "C:\Users\krivosik\Desktop\Scripts\logs". I have filtered how many words "dog" and "cat" are in all these files. Now I need to know how many words "dog" and "cat" there are in each file. For example I know there are 300 words "dog" and 250 words "cat" in all 8 files. I need to know that log1.txt includes 30 words "dog" and 20 words "cat", log2.txt includes 50 words "dog" and 10 words "cat".
I tried:
Get-ChildItem -Recurse | Select-String "dog" -List | Select Path
Upvotes: 1
Views: 27
Reputation: 437090
Assuming that each pattern (search string) only appears (at most) once per line, combine Select-String
with Group-Object
:
Get-ChildItem -File C:\Users\krivosik\Desktop\Scripts\logs |
Select-String dog, cat |
Group-Object Pattern, Path |
ForEach-Object {
[pscustomobject] @{
Pattern = $_.Group[0].Pattern
Count = $_.Group.Count
Path = $_.Group[0].Path
}
}
Sample output:
Pattern Count Path
------- ----- ----
cat 20 C:\Users\krivosik\Desktop\Scripts\logs\log1.txt
dog 5 C:\Users\krivosik\Desktop\Scripts\logs\log2.txt
# ...
Upvotes: 2