我有8个文件名为log1.txt,log2.txt,log3.txt .保存在“C:\Users\krivosik\Desktop\Scripts\log”中。我已经过滤了多少个单词“狗”和“猫”在所有这些文件。现在我需要知道每个文件中有多少个单词“狗”和“猫”。例如,我知道在所有8个文件中有300个单词“狗”和250个单词“猫”。我需要知道log1.txt包含30个单词“狗”和20个单词“猫”,log2.txt包含50个单词“狗”和10个单词“猫”。
我试过:
Get-ChildItem -Recurse | Select-String "dog" -List | Select Path发布于 2022-11-16 17:26:02
假设每个模式(搜索字符串)每一行只出现一次(最多一次),则将Select-String与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
}
}样本输出:
Pattern Count Path
------- ----- ----
cat 20 C:\Users\krivosik\Desktop\Scripts\logs\log1.txt
dog 5 C:\Users\krivosik\Desktop\Scripts\logs\log2.txt
# ...https://stackoverflow.com/questions/74464601
复制相似问题