Splunk Search

Find difference between numerical values in a Multivalue field

jaysonpryde
Path Finder

Hi,

As mentioned in the subject, I wanted to perform a simple subtraction operation on individual values/elements within a multivalue field.  So for example, I have a MV

23
79
45
23
38


I will iterate over the items, and perform the subtraction to get the following:

Iteration 1diff = 0 (since it's the first element)
Iteration 2diff = abs(79 - 23)
Iteration 3diff = abs(45 - 79)
Iteration 4diff = abs(23 - 45)
Iteration 5diff = abs(38 - 23)

 

So far, here's what I did:

| makeresults
| eval a1="23,79,45,23,29"
| makemv delim="," a1
| mvexpand a1
| eval start=0
| eval i=0
| foreach a1
  [
    eval tmp='<<FIELD>>'
    | eval diff=if(start==0, 0, "subtract_element[i+1]_with_subtract_element[i]")
    | eval i=i+1
    | eval start=1
  ]

dummy querydummy query

I haven't implemented the subtraction logic yet, since obviously, I am having a challenging time in doing it. I've spent almost 3 hours already experimenting, but no luck.

Another weird thing (for me), which I can't explain, why the variable "i" is not incrementing even if I'm updating it within the foreach block.

Hope my question makes sense, and as usual, thank you very much in advance.

Any ideas are greatly appreciated

0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@jaysonpryde 

Is this you are looking for?

| makeresults
| eval a1="23,79,45,23,29"
| makemv delim="," a1
| mvexpand a1
| autoregress p=1 a1 | eval diff=abs(a1-a1_p1)

View solution in original post

kamlesh_vaghela
SplunkTrust
SplunkTrust

@jaysonpryde 

Is this you are looking for?

| makeresults
| eval a1="23,79,45,23,29"
| makemv delim="," a1
| mvexpand a1
| autoregress p=1 a1 | eval diff=abs(a1-a1_p1)

jaysonpryde
Path Finder

Hi @kamlesh_vaghela , Thank you very much for the reply.

And yes, that's what I am looking for. I just added a :

| fillnull value=0 diff

to set 0 for the first element.

Thanks again! 

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

All Work and No Play? Not at .conf26! Unwind at These Evening Events

Between hands-on technical sessions, keynote reveals, and diving into live architectures, .conf26 is packed ...

Join the Hackathon at .conf26 and build a No-Code AI agent

Join us for the AI Agent Buildathon, an in-person, three-hour hands-on Hackathon where you’ll use Splunk Agent ...

Level Up Your Workflow: Mastering Splunk Cloud Management via Terraform

Tech Talk Recap   From Chaos to Control: Scaling Splunk Cloud with Infrastructure as Code Managing apps in ...