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!

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...