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 ...

Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...