Splunk Search

if statement in search query

7ryota
Explorer

hi all,

i would like to ask if it is possible to include IF condition in the search query

 

if msg="Security Agent uninstallation*"

[perform the below]

| rex field=msg ":\s+\(*(?<result>[^)]+)"

| table _time msg result

 

if msg="Security Agent uninstallation command sent*"

[perform the below]

| rex field=msg "^[^;\n]*;\s+\w+:\s+(?P<endpoint>.+)"

| table _time msg suser endpoint

Labels (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

The short answer is no - SPL is not a procedural language

The perhaps more helpful answer is, it depends on what you are trying to achieve. The thing to remember is that SPL processes a pipeline of events, each command in the pipeline processes the events and passes the results onto the next command in the pipeline.

Using rex to match against the values in the field can help you do conditional processing e.g. if the field contains "command sent", as in your second example, you could extract the endpoint (you may have to adjust this depending on what your events actually look like)

| rex field=msg "Security Agent uninstallation command sent.*,\s+\w+:\s+(?P<endpoint>.+)"

Events which don't have a msg field which matches the anchor string will not have the endpoint field added to them, but all the events are passed on to the next command in the pipeline.

Hopefully, that makes sense?

0 Karma

isoutamo
SplunkTrust
SplunkTrust

Hi

you cannot use if for perform any flow in SPL. But you can use several rex statements one after another and pick those values based on content of msg. Then just use those values later on or you could use if/case with eval to pick those values to anther filed if those exists e.g. eval foo=if (isnotnull(endpoint), endpoint,"N/A")

r. Ismo

0 Karma
Get Updates on the Splunk Community!

Splunk Observability Cloud’s AI Assistant in Action Series: Analyzing and ...

This is the second post in our Splunk Observability Cloud’s AI Assistant in Action series, in which we look at ...

Elevate Your Organization with Splunk’s Next Platform Evolution

 Thursday, July 10, 2025  |  11AM PDT / 2PM EDT Whether you're managing complex deployments or looking to ...

Splunk Answers Content Calendar, June Edition

Get ready for this week’s post dedicated to Splunk Dashboards! We're celebrating the power of community by ...