如何在 powershell 2.0 中的特定字符串模式附近输出额外的行?
How do you output extra lines near a specific string pattern in powershell 2.0?
我正在开发用于服务器日志监控目的的 Powershell 脚本。基本上,我需要查看日志文件并检查特定字符串模式旁边的日期。
使用以下代码,我只能找到字符串模式,但我需要在该字符串模式附近添加几行。
$fPath = "C:\temp\imlog.txt"
Get-Content $fPath | Where-Object {$_.Contains('ORA-')}
日志示例:
ORA-03135: connection lost contact
Tue Sep 26 12:49:29 2017
ORA-01775: looping chain of synonyms
Sat Sep 30 16:29:22 2017
感谢任何帮助。
谢谢
归功于@EBGreen 和@Bill_Stewart。
以下脚本将完成这项工作。
#Define log file path
$fPath = "C:\temp\imlog.txt"
#Output Context near string pattern
Get-Content $fPath | Select-String -Patter 'ORA-' -Context 2 | Select -Last 1
感谢大家。
我正在开发用于服务器日志监控目的的 Powershell 脚本。基本上,我需要查看日志文件并检查特定字符串模式旁边的日期。
使用以下代码,我只能找到字符串模式,但我需要在该字符串模式附近添加几行。
$fPath = "C:\temp\imlog.txt"
Get-Content $fPath | Where-Object {$_.Contains('ORA-')}
日志示例:
ORA-03135: connection lost contact
Tue Sep 26 12:49:29 2017
ORA-01775: looping chain of synonyms
Sat Sep 30 16:29:22 2017
感谢任何帮助。
谢谢
归功于@EBGreen 和@Bill_Stewart。
以下脚本将完成这项工作。
#Define log file path
$fPath = "C:\temp\imlog.txt"
#Output Context near string pattern
Get-Content $fPath | Select-String -Patter 'ORA-' -Context 2 | Select -Last 1
感谢大家。