Splunk Search

RegEx to Find First Match of OR

chrisboy68
Contributor

Hi, banging my head...

04/22/2016 09:23:50,865 - ERROR - exception occurred --- FOO BAR
Severity:        Error
Message:         Timeout expired. 
MachineName:     MY MACHINE


04/22/2016 09:23:56,318 - ERROR - exception occurred --- System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

I have a single log file that contains the two formats. What I want to do is if the entry has "Message:", then make this the extracted field ErrorMessage, Else If the entry have "ERROR" then, make this the extracted field ErrorMessage.

(?:Message:)|(?:ERROR -)|\s+(?P(?:[^\n]*)) this is the regex I started with and modified with few things and I still can't get it right.

Help appreciated.

Thank you,

Chris

Tags (2)
0 Karma
1 Solution

ktugwell_splunk
Splunk Employee
Splunk Employee

You could do 2 field extractions both producing the same field name ErrorMessage

props.conf

[yoursourcetype]
EXTRACT-1 = Message:\s+(?<ErrorMessage>[A-Za-z0-9 ]+)
EXTRACT-2 = \d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}:\d{2},\d{3}\s-\sERROR\s-\s(?<ErrorMessage>.+)

That regex isn't the best in the world but you can keep tweaking it to suit your needs

Be sure to add all the possible characters you may see after "Message:" in this part of the regex [A-Za-z0-9 ]

View solution in original post

0 Karma

ktugwell_splunk
Splunk Employee
Splunk Employee

You could do 2 field extractions both producing the same field name ErrorMessage

props.conf

[yoursourcetype]
EXTRACT-1 = Message:\s+(?<ErrorMessage>[A-Za-z0-9 ]+)
EXTRACT-2 = \d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}:\d{2},\d{3}\s-\sERROR\s-\s(?<ErrorMessage>.+)

That regex isn't the best in the world but you can keep tweaking it to suit your needs

Be sure to add all the possible characters you may see after "Message:" in this part of the regex [A-Za-z0-9 ]

0 Karma

chrisboy68
Contributor

Thanks, but wouldn't that still execute both EXTRACTs? I just want One or the Other.

Chris

0 Karma

ktugwell_splunk
Splunk Employee
Splunk Employee

Give it a try, I may be wrong, but I think you'll only only see EXTRACT-2 as ErrorMessage if EXTRACT-1 doesn't exist. Either that or it'll create a multi value field.

0 Karma

chrisboy68
Contributor

Thanks. You look to be correct! Its working.

Chris

0 Karma

ktugwell_splunk
Splunk Employee
Splunk Employee

Good stuff!

0 Karma
Get Updates on the Splunk Community!

Join Us for Splunk University and Get Your Bootcamp Game On!

If you know, you know! Splunk University is the vibe this summer so register today for bootcamps galore ...

.conf24 | Learning Tracks for Security, Observability, Platform, and Developers!

.conf24 is taking place at The Venetian in Las Vegas from June 11 - 14. Continue reading to learn about the ...

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...