Splunk Search

How to compare two searches using a shared variable?

ckutach
Engager

I am trying to make 2 searches using the same index and source.

The first search is looking for all entries with "message received" and the "Message Id". The second search is looking for the "Message Id" and "Message Type".  I am trying to find the number of messages received for a specific message type.

Example:

(index = exampleindex source = examplesource ("message received" AND "MessageId")  
| rex field=_raw "MessageId:(?<messageId1>[\S]+)\s.*" 
| eval Id1 = messageId1)
OR (index = exampleindex source = examplesource ("MessageType" AND "MessageId") 
| rex field=_raw "MessageId:(?<messageId2>[\S]+)\s.*" 
| eval Id2 = messageId2)

 I want to compare the 2 searches and count how many Ids in the 1st search appear in the 2nd search

 

Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

The OR operator works within certain commands, but cannot be used to combine commands.

Use OR within the base search to fetch events with the desired data then group them by the shared field.

(index = exampleindex source = examplesource (("message received" AND "MessageId") OR ("MessageType" AND "MessageId"))
| rex field=_raw "MessageId:(?<messageId>[\S]+)" 
``` Combine events based on shared messageId values ```
| stats values(*) as * by messageId
``` Discard events without a MessageType ```
| where isnotnull(MessageType)
| stats count by MessageType

 

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

View solution in original post

0 Karma

richgalloway
SplunkTrust
SplunkTrust

The OR operator works within certain commands, but cannot be used to combine commands.

Use OR within the base search to fetch events with the desired data then group them by the shared field.

(index = exampleindex source = examplesource (("message received" AND "MessageId") OR ("MessageType" AND "MessageId"))
| rex field=_raw "MessageId:(?<messageId>[\S]+)" 
``` Combine events based on shared messageId values ```
| stats values(*) as * by messageId
``` Discard events without a MessageType ```
| where isnotnull(MessageType)
| stats count by MessageType

 

---
If this reply helps you, Karma would be appreciated.
0 Karma
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!

Build the Future of Agentic AI: Join the Splunk Agentic Ops Hackathon

AI is changing how teams investigate incidents, detect threats, automate workflows, and build intelligent ...

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...