string - 使用 PowerShell 计算 log.file 中字符串的出现次数
问题描述
我需要在日志文件中出现“失败”。问题是我需要为每个会话块获取“失败”的发生。日志的样子:
---第一节 查看 查看 失败 失败 查看 ----第二场 查看 失败 查看 查看 ----第三节 失败 失败
到目前为止,我得到的是:
$rows = Get-Childitem -Path E:\shell\lot.log |
Select-String -Pattern failure
$i = 0
foreach ($row in $rows) {
$i++
}
echo $i
使用该脚本,我只能得到出现的总数。
解决方案
每当出现以 3 个或更多连续连字符开头的行并将结果收集在哈希表中时,我都会启动一个新计数器。
$failcount = @{}
Get-Content 'E:\shell\lot.log' | ForEach-Object {
if ($_ -match '^-{3,}(.*)') {
$section = $matches[1].Trim()
$failcount[$section] = 0
} elseif ($_ -like '*failure*') {
$failcount[$section]++
}
}
推荐阅读
- c++ - 从 GLTF 文件中获取基元总数
- spring-cloud-vault-config - spring-cloud-vault:最小续订与到期阈值
- apache-spark - 如何查找在火花文件流中处理的文件
- asp.net-mvc - aspnetcore 多个全局异常过滤器问题
- ruby-on-rails - 如何在没有 Active Storage 的情况下安装 Action Text?
- c# - StackExchange.Redis - 配置文件同步方法
- flutter - 有没有一种简单的方法可以使 android 后退按钮的行为与应用栏后退按钮完全一样?
- r - 如何绘制与分类数据的交互(示例)
- python - Python Groupby 函数
- operating-system - 页面替换和 LRU