Reputation: 35
I want to extract all strings from "Timestamp:" to "Parameter"
If I encounter any String ending with "Exception:" between "a\n" to "site" where '-' is the character preceding "\nTimestamp"
Input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Timestamp: 8/10/2017 3:11:53 AM
Message: System.ArgumentNullException: site parameter is missing from distributed cache namespace
Parameter name: site
Output:
Timestamp: 8/10/2017 3:11:53 AM
Message: System.ArgumentNullException: site parameter is missing from distributed cache namespace
Basically, I want to detect any Exception in my .log file and show the Timestamp when that exception occured. Would be happy if the regex works from the Windows Command Line. Thank you.
Upvotes: 1
Views: 43
Reputation: 29677
Here's a regex that'll capture the timestamp and the message content in capture group 1 & 2 :
/^Timestamp:\s*(.*)$\s*Message:\s*(.*)$/gm
You can test it here
Example of powershell running the regex on the command line for a file "test.txt":
powershell -Command "$f=[io.file]::ReadAllText('test.txt'); $f |Select-String '(?m)^Timestamp:\s*(.*)$\s*Message:\s*(.*)$' -AllMatches |%{$_.Matches}|%{$_.Groups[1].Value+': '+$_.Groups[2].Value}"
Upvotes: 0