Splunk Search

How to dynamically subtract two last column values?

chrisboy68
Contributor

Hi, have  SPL that generates months of data. I want subtract just the last two columns. The fields will change month to month, so I can't hard code.

Given the below sample, how can I get lastMonthDiff without hardcoding the field values? Thank you! Chris

 

 

 

| makeresults 
| eval "2202-01"=1 
| eval "2202-02"=2 
| eval "2202-03"=5 
| eval "2202-04"=4 
| append 
    [| makeresults 
    | eval "2202-01"=4 
    | eval "2202-02"=5 
    | eval "2202-03"=7 
    | eval "2202-04"=3
        ] 
| append 
    [| makeresults 
    | eval "2202-01"=5 
    | eval "2202-02"=2 
    | eval "2202-03"=7 
    | eval "2202-04"=9
        ] 
| fields - _time |foreach *  [eval lastMonthDiff = '2202-03' - '2202-04'] 

 

 

 

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| foreach *
    [| eval diff=if(isnull(previous),null(),previous-'<<FIELD>>')
    | eval previous='<<FIELD>>']

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust
| foreach *
    [| eval diff=if(isnull(previous),null(),previous-'<<FIELD>>')
    | eval previous='<<FIELD>>']

chrisboy68
Contributor

Awesome, thank you!

0 Karma
Get Updates on the Splunk Community!

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...

Detecting Remote Code Executions With the Splunk Threat Research Team

REGISTER NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If ...