如何在日志文件集合中搜索字符串数组

huangapple go评论65阅读模式
英文:

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

你可以很容易地完成这个任务:

  1. 使用 -AllMatches 开关使 Select-String 输出在给定行内的 所有匹配项
  2. 遍历结果对象上的 Matches 属性,以获取单独匹配的名称
  3. 使用 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:

  1. Use the -AllMatches switches to make Select-String output all matches within a given line
  2. Iterate over the Matches property on the resulting object to get the individually matched names
  3. 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
}

huangapple
  • 本文由 发表于 2023年7月6日 17:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定