Splunk Search

What is the best way to do calculations for individual values in multiple columns?

yvassilyeva
Path Finder

Hi! i have the following table:

Com-JunCom-JulCom-AugJunJulAug
331257
113253

 

Can i add the values of each column starting with Com to the values of month. (Com-Jun plus June, Com-Jul plus July etc.) So the values in the first 3 columns should be added to the values of the last three columns so that my table looks like this:

Com-JunCom-JulCom-AugJuneJulyAugustSum-JunSum-JulSum-Aug
331257588
113253366

 

Thank you!

Labels (2)
0 Karma
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi @yvassilyeva,

you have to use the eval command:

| eval "Sum-Jun"="Com-Jun"+June, "Sum-Jul"="Com-Jul"+July, "Sum-Aug"="Com-Aug"+August

One hint: don't use "-" in the field names, use "_".

Ciao.

Giuseppe

View solution in original post

0 Karma

niketn
Legend

@yvassilyeva please add the following to your existing search and it should do a template foreach for all months

<yourCurrentSearch>
| foreach Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec [| eval "Sum-<<FIELD>>"='<<FIELD>>'+'Com-<<FIELD>>']

Also more specific answer can be provided based on whether your SPL is calculating these columns or do these exist in your event.

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

yvassilyeva
Path Finder

@niketn 

I'm not getting the results. What if i have a table like this:

Comp_JunComp_JulComp_AugTar_JunTar_JulTar_Aug
331257
113253

and need to get the sum of Comp_Jun and Tar_Jun, Comp_Jul and Tar_Jul, Comp_Aug and Tar_Aug. My current code is this:

| foreach Tar_Jan,Tar_Feb,Tar_Mar,Tar_Apr,Tar_May,Tar_Jun,Tar_Jul,Tar_Aug,Tar_Sep,Tar_Oct,Tar_Nov,Tar_Dec [| eval "Sum_<<FIELD>>"='<<FIELD>>'+'Comp_<<FIELD>>']

0 Karma

niketn
Legend

@yvassilyeva thanks for adding the details. I wished you mentioned in your question and your SPL could have been easier... Do read the foreach command Splunk Documentation if the interpretation of the template is not clear. It has solid examples and that is where I learnt these from. https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Foreach

| foreach "Com-*"
    [| eval "Sum-<<MATCHSTR>>"='Tar-<<MATCHSTR>>'+'<<FIELD>>']

Following is a run anywhere search based on sample data provided so that you can play around yourself.

| makeresults 
| eval data="3,3,1,2,5,7;1,1,3,2,5,3" 
| makemv data delim=";" 
| mvexpand data 
| makemv data delim="," 
| eval Com-Jun=mvindex(data,0),Com-Jul=mvindex(data,1),Com-Aug=mvindex(data,2),Tar-Jun=mvindex(data,3),Tar-Jul=mvindex(data,4),Tar-Aug=mvindex(data,5) 
| table *Jun, *Jul, *Aug 
| foreach "Com-*"
    [| eval "Sum-<<MATCHSTR>>"='Tar-<<MATCHSTR>>'+'<<FIELD>>']
| fields Com-* Tar-* Sum-*

 

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

yvassilyeva
Path Finder

Thank you so much, @niketn !

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @yvassilyeva,

you have to use the eval command:

| eval "Sum-Jun"="Com-Jun"+June, "Sum-Jul"="Com-Jul"+July, "Sum-Aug"="Com-Aug"+August

One hint: don't use "-" in the field names, use "_".

Ciao.

Giuseppe

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @yvassilyeva,

Ciao and happy splunking.

Giuseppe

P.S.: Karma Points are appreciated 😉

0 Karma

yvassilyeva
Path Finder

Thank you!

0 Karma

yvassilyeva
Path Finder

Giuseppe,

Thank you for a quick response. Is there a way to calculate that for all three columns at once, instead of going month by month? It would be something like:

eval totals=Comp*+months

 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @yvassilyeva,

not in my knowledge!

Ciao.

Giuseppe

0 Karma
Get Updates on the Splunk Community!

Observability Unlocked: Kubernetes Monitoring with Splunk Observability Cloud

  Ready to master Kubernetes and cloud monitoring like the pros?Join Splunk’s Growth Engineering team for an ...

Wrapping Up Cybersecurity Awareness Month

October might be wrapping up, but for Splunk Education, cybersecurity awareness never goes out of season. ...

🌟 From Audit Chaos to Clarity: Welcoming Audit Trail v2

&#x1f5e3; You Spoke, We Listened  Audit Trail v2 wasn’t written in isolation—it was shaped by your voices.  In ...