Splunk Search

How to compare last value with the second last value?

ariyazudeen
New Member

Say I have a column with 5 records in it
88
22
67
44
55

I want to compare the last record 55 with that of second last value and want to write whether the value was 'greater' or 'smaller' in the output.

In above case 55 was greater so my output should say GREATER. Do we have any command to accomplish this?

Tags (1)
0 Karma

knielsen
Contributor

Maybe a combination of tail and delta? Assuming the fieldname of the column was "val":

| inputlookup test.csv | tail 2 | delta val as delta | eval output=case(delta<0,"GREATER",delta>0,"LESSER",true(),"EQUAL") | fields output | tail 1

Hth,
Kai.

edit: damn, 32secs late... 😉

ariyazudeen
New Member

Can you explain how this above query works. It is working but I'm not sure how if you compare (output=case(delta<0,"GREATER",delta>0,"LESSER",true(),"EQUAL") with Zero value it is giving the desire result.

0 Karma

ariyazudeen
New Member

I got it now. Thanks 🙂

0 Karma

sundareshr
Legend

Try this

base search | tail 2 | delta field as difference | eval greater=if(difference>0, "Yes", "no")

*OR*

base search | delta field as difference | reverse | eval greater=if(difference>0, "Yes", "no")
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...