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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...