Splunk Search

Can I do a calculation inside an IF statement's True condition?

khinnway
Engager

I need to find the power consumption of each day using the cumulative power meter reading;
Today's reading - Yesterday's reading = Yesterday's consumption (Diff)

However, the meter reading resets after it reaches 999, 999. Thus, if the meter reading resets, the consumption will appear as a large negative value. To get the correct value, I will have to add 1,000,000 (as 0 is a count as well).

I am very used to C programming and new to Splunk so please forgive me. This is the line I came up with:
eval Diff=if (Diff<0, Diff=Diff+10000000, Diff)

The value of Diff disappears completely for the day that reset occurs. I do realize that calculations are not exactly allowed inside an IF statement, but may I know how to go around it?

Thank you in advance!

0 Karma
1 Solution

renjith_nair
Legend

@khinnway ,

Yes it's possible to do calculation in condition. Please find below a dummy search . The result should be 0

|makeresults|eval Diff=-100|eval Diff=if(Diff<0,Diff+100,Diff)

If still does not work, please share your SPL (mask any confidential data)

---
What goes around comes around. If it helps, hit it with Karma 🙂

View solution in original post

renjith_nair
Legend

@khinnway ,

Yes it's possible to do calculation in condition. Please find below a dummy search . The result should be 0

|makeresults|eval Diff=-100|eval Diff=if(Diff<0,Diff+100,Diff)

If still does not work, please share your SPL (mask any confidential data)

---
What goes around comes around. If it helps, hit it with Karma 🙂

khinnway
Engager

Thank you so much for the help! I really appreciate the fast comment!

Get Updates on the Splunk Community!

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

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

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...