Splunk Search

How to write regex to select specific fields on a search?

carlyleadmin
Contributor

hi below is my search,

when I do search for Error this is what I get;
alt text

then I run this search to create "Message" field and it brings up from ERROR to ***Exception message*

index=pa host=aptr45 sourcetype=Service| transaction startswith="Error" endswith="ERROR" maxevents=15000 | rex "ERROR (?.*)"|table host,Message,_time

I basically want to bring everything starting from ERROR to all the way down to the last line. How do I do that?

I also wanted to extract another field starting from **Exception message** to ***Stack Trace* but it doesn't work.any help is appreciated.

for that, I tried using multiple combinations and I was not successful. i tried using; |rex " Exception message (?.Stack trace)", |rex " *Exception message* (?.*Stack trace)", |rex " Exception message (?.*)",

I tried using field extractor to address the issue but I guess it was to complex that it kept giving me error while trying to extract it.

I've never written regex manually before so please be patient with me:)

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

A most excellent resource for writing regular expressions is regex101.com. There you can paste some sample data then try various regex strings to see what matches.

To get Splunk to match text across lines, use [\s\S]* instead of .*. So your first search would become index=pa host=aptr45 sourcetype=Service| transaction startswith="Error" endswith="ERROR" maxevents=15000 | rex "ERROR (?<foo>[\s\S]*)"| table host,Message,_time.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

A most excellent resource for writing regular expressions is regex101.com. There you can paste some sample data then try various regex strings to see what matches.

To get Splunk to match text across lines, use [\s\S]* instead of .*. So your first search would become index=pa host=aptr45 sourcetype=Service| transaction startswith="Error" endswith="ERROR" maxevents=15000 | rex "ERROR (?<foo>[\s\S]*)"| table host,Message,_time.

---
If this reply helps you, Karma would be appreciated.

carlyleadmin
Contributor

Thank you richgalloway.i will definitely check regex101.

quick question if i wanted to extract 2 fields,first one would be the lines starting from Error to *Exception Message* and second would be the lines between **exception message to the the last line what would be the search look like.thanks for all the help

0 Karma

richgalloway
SplunkTrust
SplunkTrust

That's probably best done with two rex commands.

index=pa host=aptr45 sourcetype=Service| transaction startswith="Error" endswith="ERROR" maxevents=15000 | rex "ERROR (?<foo>[\s\S]*)"| rex "ERROR (?<bar>.*)" | table host,Message,_time
---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...