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!

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...