powershell - 在 PowerShell 中跨多行匹配日志条目
问题描述
我正在尝试搜索包含跨越多行的日志条目的日志文件。例子:
BEGIN
...
123456
...
END
BEGIN
...
456789
...
END
我想搜索一个特定的数字 (123456),但打印从前面的 BEGIN 标记到 END 标记的所有内容。如何在 PowerShell 中完成此操作?
我尝试了几个正则表达式,但没有得到它的工作。我到目前为止的代码是:
$id = '123456'
$pattern = 'BEGIN(.|\s)*?'+$id+'(.|\s)*?END'
$matches = Select-String -Path "C:\temp\logile.log" -Pattern $pattern
不知道为什么这不起作用。有没有其他方法可以让它工作?
解决方案
我会将日志分成多个部分,并进行回顾
foreach ($Section in (Get-Content C:\temp\logile.log -raw) -split '(?<=END\r?\n?)' -ne ''){
if($section -match '123456'){
$Section
}
}
样本输出:
BEGIN
...
123456
...
END
推荐阅读
- php - RHEL 无法通过 PHP 代码连接 MySQL 数据库
- android - 如何在 TabLayout.Tab 中设置自定义视图
- java - java.lang.Exception 错误:测试类应该只有一个公共构造函数
- python - Alexa 技能 Python Flask ask 和 SocketIO with Unity
- java-8 - 在 ubuntu 上安装 jdk8 时出错
- python-3.x - 如何使小部件适合整个框架以在 tkinter 中调整大小?
- scala - Spark Scala withColumn getItem
- docker - Hyperledger Fabric:定义 docker-compose 时要使用的 MSP 目录
- java - 根据 queryurl 中存在的键,将字符串替换为完全匹配的单词
- ios - 获取距离 eddystone 信标