查找从哪个文件是文本powershell .txt的一部分



我有8个文件名为log1.txt, log2.txt, log3.txt…保存在"C:UserskrivosikDesktopScriptslog "我已经过滤了多少单词"dog"one_answers";cat"都在这些文件里。现在我需要知道有多少单词"dog"one_answers";cat"每个文件中都有。例如,我知道有300个单词"狗";250字的"猫"字;在所有8个文件中。我需要知道log1.txt包含30个单词"dog"20个单词的"猫",log2.txt包含50个单词的"狗"。还有10个词<;cat"

我试着:

Get-ChildItem -Recurse | Select-String "dog" -List | Select Path

假设每个模式(搜索字符串)每行只出现(最多)一次,将Select-StringGroup-Object组合:

Get-ChildItem -File C:UserskrivosikDesktopScriptslogs | 
Select-String dog, cat |
Group-Object Pattern, Path |
ForEach-Object {
[pscustomobject] @{
Pattern = $_.Group[0].Pattern
Count = $_.Group.Count
Path = $_.Group[0].Path
}
}

样本输出:

Pattern Count Path
------- ----- ----
cat        20 C:UserskrivosikDesktopScriptslogslog1.txt
dog         5 C:UserskrivosikDesktopScriptslogslog2.txt
# ...

最新更新