Splunk Search

Can I perform If-Then-Else logic within a search?

maverick
Splunk Employee
Splunk Employee

I have a field that I want to report on, but in some of my events, that field is missing (null) and so I'd like to use another field value instead. Is there a way to perform an in-line "If-Then-Else" type logic within the search itself?

cyue_splunk
Splunk Employee
Splunk Employee

I'll use " | eval coalesce... "

rshoward
Path Finder

I know the question is old, and the question was about if then else, but OP was really asking for coalesce and this is the right answer here. | eval field=coalesce(field1,field2,"misc") will return assign field to the first non-null result in a list. e.g. field1 if its not null, if it is null coalesce will return field2, if that is null as well, the field will just be the string "misc".

0 Karma

maverick
Splunk Employee
Splunk Employee

Also, see this page in the Splunk docs for more details regaring the "eval" command:

http://docs.splunk.com/Documentation/Splunk/5.0/SearchReference/Eval

Also, see this page regarding the functions that can be used with the "eval" command:

http://docs.splunk.com/Documentation/Splunk/5.0/SearchReference/CommonEvalFunctions

steveyz
Splunk Employee
Splunk Employee

... | eval field=if(isnull(field),elsefield,field) | ...

or use directly within a stats

... | stats sum(eval(if(isnull(field),elsefield,field))) as mysum ...

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Take Action Automatically on Splunk Alerts with Red Hat Ansible Automation Platform

 Are you ready to revolutionize your IT operations? As digital transformation accelerates, the demand for ...

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...