Splunk Search

how to write an "if" statement for a value in all the fields?

mahbs
Path Finder

Hi,

Is there a way of writing an if condition that basically says, "if value x exists in all of tabled fields, then create a new field, and insert the value "valid" into it".

Is that possible?

0 Karma

micahkemp
Champion

It sounds like you're asking that all fields present be equal to the same value (for my search that value will be rightvalue). If that is the case, you can use foreach to check the value of each field, and use some additional logic to accomplish what you are looking for. This run anywhere example shows this in action:

| makeresults 
| eval fieldA="wrongvalue", fieldB="rightvalue", fieldC="rightvalue", fieldD="wrongvalue"
| append [| makeresults | eval fieldA="rightvalue", fieldB="rightvalue", fieldC="rightvalue", fieldD="rightvalue"]
| fields - _time
| foreach * [eval validity=if((isnull(validity) or validity="valid") and <<FIELD>>="rightvalue", "valid", "invalid")]

If you need the new field to be null instead of "invalid", you could add | eval validity=if(validity="valid", validity, NULL)

niketn
Legend

@micahkemp, should the isnull(is_valid) be isnull(validity)?

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

micahkemp
Champion

Yes, thanks, and corrected above. I changed my field names after pasting the search into answers, and I missed that one. Annoyingly the incorrect search actually gave the correct results for those specific test cases.

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

hi @mahbs,

Basically you can use Foreach command for search. Your search like below..

YOUR_SEARCH | eval valid="true" | foreach "*"  [ eval valid=if('<<FIELD>>'=="x","true","false") ] 

https://docs.splunk.com/Documentation/Splunk/7.0.1/SearchReference/Foreach

I hope this will help you.

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