Splunk Search

Reduce a result set using the foreach splunk search command

lpolo
Motivator

The result of a splunk query is the following:

Result set 1:

method  success failures    Over_method1    Over_method2    Over_method3
method1 0   73               3                 0            0
method2 196 0                0                 2            0

I need to reduce this result set as follow:

method  success failures    Over
method1    0    73            3
method2   196   0             2

I tried using the search command foreach but no success.

Splunk search to get result set 1|
foreach Over_* [eval Over=Over+<<FIELD>>]|
table method success failures Over 

OR

Splunk to get result set 1|
foreach Over_* [eval Over=Over+'<<FIELD>>']|
table method success failures Over 

This could be done with the following query:

Splunk search to get result set 1|
=if(Over_method1>0,Over_method1,if(Over_method2>0,Over_method2,0))|
table method success failures Over 

However, A simpler way is to use the foreach function. To make it work the mapping variable needs to be initialized as presented in below answer. If not the result set will not be reduced correctly. So the final query is:

Splunk to get result set 1|eval Over=0|
foreach Over_* [eval Over=Over+'<<FIELD>>']|
table method success failures Over 

Thanks,
Lp

Tags (1)
0 Karma

lpolo
Motivator

However, After reading the answer presented bellow I made a little change for the eval expression to work, <> needs to be surrounded by single quotes.

0 Karma

somesoni2
Revered Legend

Try this

Splunk search to get result set 1| eval Over=0 | foreach Over_* [eval Over=Over + <<FIELD>>]|table method success failures Over

Alternative (using untable)

Splunk search to get result set 1 | untable method key value  | eval key=case(like(key,"Over%"),"Over",1=1,key) | chart sum(value) over method by key

lpolo
Motivator

Thanks. I made a little change for the eval expression to work, <> needs to be surrounded by single quotes. I updated the original question.

|Splunk search to get result set 1|eval Over=0 | foreach Over_* [eval Over=Over+'<>']|table method success failures Over

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...

Network to App: Observability Unlocked [May & June Series]

In today’s digital landscape, your environment is no longer confined to the data center. It spans complex ...

SPL2 Deep Dives, AppDynamics Integrations, SAML Made Simple and Much More on Splunk ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...