英文:
How to search for an array of strings in a collection of log files
问题
我有一个名称数组,我想在多个日志文件中查找这些名称的出现次数。下面的代码有效,但它只返回日志中出现任何名称的所有行。
我想要获得名称在日志文件中的出现次数的频率表,或者只是每个名称的二进制值,指示该名称是否在任何日志文件中出现过。
是否有可能?
英文:
I have an array of names, and I want to find occurrences of those names in multiple log files. The code below works, but it only returns all the lines in the log where any of the names occur.
$array_of_names = 'name1','name2','etc'
$log_path = 'path_containing_log_files'
$logs = Get-ChildItem $log_path
foreach($log in $logs){
Select-String -Path $file -Pattern $array_of_names
}
I want to get either a frequency table for the occurrences of the names in the log files, or just binary value per name that tells if the name ever occurred in any of the log files.
Is it possible?
答案1
得分: 0
你可以很容易地完成这个任务:
- 使用
-AllMatches
开关使Select-String
输出在给定行内的 所有匹配项 - 遍历结果对象上的
Matches
属性,以获取单独匹配的名称 - 使用
Group-Object
生成一个频率表
$array_of_names = 'name1','name2','等等'
$log_path = '包含日志文件的路径'
$logs = Get-ChildItem $log_path -File
# 搜索所有日志文件并提取其中找到 _任何_ 名称的日志文件
$results = $logs | Select-String -Pattern $array_of_names -AllMatches
$matchingLogFiles = $results | ForEach-Object -MemberName Filename
# 根据值对匹配的子字符串进行分组
$frequencyTable = $results | ForEach-Object -MemberName Matches | Group-Object Value -AsHashtable
foreach($key in $frequencyTable.psbase.Keys){
# 将分组输出表转换为频率表,将每个条目映射到其计数
$frequencyTable[$key] = $frequencyTable[$key].Count
}
这就是你想要的代码部分的翻译。
英文:
> I want to get either a frequency table for the occurrences of the names in the log files [...]
You can get that pretty easily:
- Use the
-AllMatches
switches to makeSelect-String
output all matches within a given line - Iterate over the
Matches
property on the resulting object to get the individually matched names - Use
Group-Object
to generate a frequency table
$array_of_names = 'name1','name2','etc'
$log_path = 'path_containing_log_files'
$logs = Get-ChildItem $log_path -File
# search all log files and extract the names of the log files where _any_ name was found
$results = $logs |Select-String -Pattern $array_of_names -AllMatches
$matchingLogFiles = $results |ForEach-Object -MemberName Filename
# group matching substrings based on value
$frequencyTable = $results |ForEach-Object -MemberName Matches |Group-Object Value -AsHashtable
foreach($key in $frequencyTable.psbase.Keys){
# turn the grouped output table into a frequency table by mapping each entry to its count
$frequencyTable[$key] = $frequencyTable[$key].Count
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论