0

I'm trying to find lines starting from one of many patterns in many files (log parsing).
Is there any better solution than:

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

The above script unfortunately searches only for one pattern, and using Where-Object I don't have the line number in which the pattern was found.

Vexator
  • 181
  • 3
  • 9
  • 1
    You don't need to use `Get-Content`. `Select-String` takes a path with a wildcard by itself. ;-) – Olaf Apr 27 '20 at 14:42
  • 1
    ..and if you;re using RegEx, just seperate the strings to match? Select-String -Pattern "String1|String2" – Scepticalist Apr 27 '20 at 17:02

1 Answers1

0

Looks like I made it unnecessarily complicated ;)

Select-String -Path *DBLog.txt -Pattern '^Msg|^Warn' |
Select-Object FileName, LineNumber, Line |
Out-GridView

but I have an additional question… how do you add an extra line to that? What I am looking for (SQL Server errors) is written in 2 lines:

Msg 208, Level 16, State 1, Server LOCALHOST, Line 9
Invalid object name …

while the Select-String will only return the first line

Vexator
  • 181
  • 3
  • 9