Splunk Search

Grouping the messages based on 2 fields in splunk?

ghostrider
Path Finder

I have below events/messages in my search result. There are 2 fields stack_trace and TYPE like below. I want to group the events and count them as shown below based on a particular text from stack_trace and TYPE field as below. Is it possible to group the messages based on 2 fields (TYPE,stack_trace)? I am using below query but I am stuck as to how to group by 2 fields. 

Event 1

 

 

{
TYPE: ABCD
stack_trace : com.abc.xyz.package.ExceptionName: Missing A.
at random.package.w(DummyFile1:45)
at random.package.x(DummyFile2:64)
at random.package.y(DummyFile3:79)
}

 

 

 

Event 2

 

 

{
TYPE: XYZ
stack_trace : com.abc.xyz.package.ExceptionName: Missing B.
at random.package.w(DummyFile1:45)
at random.package.x(DummyFile2:64)
at random.package.y(DummyFile3:79)
}

 

 

 

Expected Output

 

 

TYPE   Exception  Count
ABCD   Missing A   3
ABCD   Missing B   4
XYZ    Missing A   6
XYZ    Missing B   1

 

 

 

Query I am using but incomplete 

 

 

BASE_SEARCH
| rex field= _raw "Exception: (?<Exception>[^\.\<]+)"
| stats count as Count by "Exception"

 

 

 

Actual Output

 

 

Exception  Count
Missing A   3
Missing B   4
Missing c   6

 

 

 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @ghostrider,

yes you can use more than one field in the stats command, so you could use:

BASE_SEARCH
| rex field= _raw "Exception: (?<Exception>[^\.\<]+)"
| stats count as Count by TYPE "Exception"

You have to put attention only to one thing: using two fields in the stats command you take only the events with both the fields, in other words, if one event has the TYPE field but not the TYPE field or opposite, the event isn't counted.

So you have to analyze your data to be sure that all the relevant events have both the fields.

If one of them could be missing, use fillnull command to assign a value (e.g. "-") when the value is missing.

Ciao.

Giuseppe

ghostrider
Path Finder

Thanks!! I just ran the query, and it looks like it shows "null" in the final output when the TYPE fields is not present but stack_trace is present. Is it possible to replace the null with some string like "N/A" so that it looks cleaner. 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @ghostrider,

as I said, you can use the fillnull command (https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Fillnull

BASE_SEARCH
| rex field= _raw "Exception: (?<Exception>[^\.\<]+)"
| fillnull value="N/A" TYPE
| stats count as Count by TYPE "Exception"

Ciao.

Giuseppe

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...