Splunk Search

Need help with Splunk Query

vinoths_82
Explorer

Hi

What I have:

I have a list of events with multiple <Key,Value> pairs. For eg., like below

event1:attributes:{"test__c":90, "abc":10,"now__c":10 }

event2:attributes:{"bcf:90, abc:10}

event1:attributes:{"testing__c":10, "abc":10,"now__c":100 }

As you can, some rows have attributes fields with a substr "__c" and the rest do not. 

What I want:

Now, I want to iterate over a list of events and calculate a total of all field values (fields which has substr __c) and display that in a table format like below

Total__C

100. (Since event 1 has 2 __c substr fields, their values add to 100)

0 (event2 has no __c fields, so default to 0)

110 (event3 has 2 fields)

What I tried:

I used foreach like below for 1 event, but I am not sure how to do it for all events iteratively.

fields * | foreach *__c [ eval TotalCustom = TotalCustom + '<<FIELD>>'] | table TotalCustom

Can someone help me with this?

Labels (1)
Tags (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Your solution was very close and the fix was not obvious.  This run-anywhere example works with your sample data, however.

| makeresults | eval data="event1:attributes:{\"test__c\":90, \"abc\":10, \"now__c\":10 }|
event2:attributes:{\"bcf\":90, \"abc\":10 }|
event1:attributes:{\"testing__c\":10, \"abc\":10, \"now__c\":100 }" | eval data=split(data,"|") | mvexpand data | eval _raw=data | extract pairdelim="{, ", kvdelim=":"
```Above just defines test data```
| eval TotalCustom = 0 
| foreach *__c [ eval TotalCustom = TotalCustom + coalesce('<<FIELD>>', 0)]
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

Your solution was very close and the fix was not obvious.  This run-anywhere example works with your sample data, however.

| makeresults | eval data="event1:attributes:{\"test__c\":90, \"abc\":10, \"now__c\":10 }|
event2:attributes:{\"bcf\":90, \"abc\":10 }|
event1:attributes:{\"testing__c\":10, \"abc\":10, \"now__c\":100 }" | eval data=split(data,"|") | mvexpand data | eval _raw=data | extract pairdelim="{, ", kvdelim=":"
```Above just defines test data```
| eval TotalCustom = 0 
| foreach *__c [ eval TotalCustom = TotalCustom + coalesce('<<FIELD>>', 0)]
---
If this reply helps you, Karma would be appreciated.

vinoths_82
Explorer

Thanks for the answer. 

0 Karma
Get Updates on the Splunk Community!

AI for AppInspect

We’re excited to announce two new updates to AppInspect designed to save you time and make the app approval ...

App Platform's 2025 Year in Review: A Year of Innovation, Growth, and Community

As we step into 2026, it’s the perfect moment to reflect on what an extraordinary year 2025 was for the Splunk ...

Operationalizing Entity Risk Score with Enterprise Security 8.3+

Overview Enterprise Security 8.3 introduces a powerful new feature called “Entity Risk Scoring” (ERS) for ...