Splunk Search

Combine the Sum of Two Fields

bcarr12
Path Finder

Hi all,

For a search similar to the following:
index=myindex "Search Term" NOT field=value source="mylog.log" | eval totalx=aCount+bCount | stats sum(totalx) by y | sort -sum(totalx)

Splunk returns exactly the data I am looking for. A table of sum(totalx) by y (of which there are around 5 different values of y). I have a request to combine the sum(totalx) values for 2 of the 5 values and treat them as one value but leave the rest unchanged. What would be the best way to accomplish this?

For instance, right now my search returns a table similar to this:
y sum(totalx)
1 10
2 20
3 30
4 40
5 50

I am essentially trying to create an additional field, let's call it 45, which represents the sum of 4 and 5 at all times. So instead, the data being visualized is:
y sum(totalx)
1 10
2 20
3 30
45 90

Thanks!

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Combine 4 and 5 before calling stats.

index=myindex "Search Term" NOT field=value source="mylog.log" 
| eval totalx=aCount+bCount, y=case(y=4 OR y=5, 45, 1==1, y) 
| stats sum(totalx) by y | sort -sum(totalx)
---
If this reply helps you, Karma would be appreciated.
0 Karma

bcarr12
Path Finder

Hmm, this doesn't seem to do anything. Although I think it doesn't work because if I don't do stats I don't get the numerical values to combine. Stats is what causes the totalx by y to become available. Before I run stats y is just a text field=value pair that is non numerical if that makes sense?

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Field y must exist before stats or you'll get no results.
The case function in my answer is doing the combining. It doesn't need numeric values (if you have nonnumeric values, use quotation marks (y="d" OR y="e", "de")). It produces data like this:

1 10
2 20
3 30
45 40
45 50

Then stats can do the sums to produce

1 10
2 20
3 30
45 90
---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...