【发布时间】:2020-08-11 03:39:25
【问题描述】:
我正在尝试从许多文件中的许多模式之一开始查找行(日志解析)。
有没有比这更好的解决方案:
Get-ChildItem -Filter *DBLog.txt | ForEach-Object {
$name = $_.Name
Get-Content -Path $_.FullName |
Select-String -Pattern '^Msg*' |
Select-Object @{Name='File Name'; Expression={$name}}, LineNumber, Line
} |
Out-GridView
不幸的是,上面的脚本只搜索一种模式,并且使用Where-Object 我没有找到该模式的行号。
【问题讨论】:
-
你不需要使用
Get-Content。Select-String本身采用带有通配符的路径。 ;-) -
..如果您正在使用正则表达式,只需分隔字符串以匹配?选择字符串-模式“String1|String2”
标签: powershell foreach get-childitem select-string foreach-object