Splunk Search

How to use Foreach using multiple columns?

D2SI
Communicator

Hello there,

I am having a hard time figuring out how to use / how is working foreach + eval.

I have something like :

alt text

Which can be obtained using this search:

| makeresults
| eval a="test"
| eval result_a_version_1=1
| eval result_a_version_2=3
| eval result_b_version_1=5
| eval result_b_version_2=2
| fields - _time
| table a result_a_version_1 result_b_version_1 result_a_version_2 result_b_version_2

And I am trying to use foreach/eval to get this :

alt text

Matching search :

| makeresults
| eval a="test"
| eval result_a_version_1=1
| eval result_a_version_2=3
| eval result_b_version_1=5
| eval result_b_version_2=2
| fields - _time
| eval tota_a_b_version_1=result_a_version_1+result_b_version_1
| eval tota_a_b_version_2=result_a_version_2+result_b_version_2
| table a result_a_version_1 result_b_version_1 tota_a_b_version_1 result_a_version_2 result_b_version_2 tota_a_b_version_2

As the version number can vary, I am guessing I have to use foreach somehow.

Thanks in advance for any tip!

0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi @D2SI,

I'm able to total of similar version. Can you please try the following search. I'm still working on the field name.

| makeresults 
| eval test="test" 
| eval result_a_version_1=1 
| eval result_a_version_2=3 
| eval result_b_version_1=5 
| eval result_b_version_2=2
| eval result_a_version_3=50 
| eval result_b_version_3=20 
| fields - _time 
| table test result_a_version_1 result_b_version_1 result_a_version_2 result_b_version_2 result_a_version_3 result_b_version_3
| foreach result_*_version_* 
    [ eval total_of_version_<<MATCHSEG2>>=if(isnotnull(total_of_version_<<MATCHSEG2>>),total_of_version_<<MATCHSEG2>>+'<<FIELD>>','<<FIELD>>') ] 

Thanks

View solution in original post

kamlesh_vaghela
SplunkTrust
SplunkTrust

Hi @D2SI,

I'm able to total of similar version. Can you please try the following search. I'm still working on the field name.

| makeresults 
| eval test="test" 
| eval result_a_version_1=1 
| eval result_a_version_2=3 
| eval result_b_version_1=5 
| eval result_b_version_2=2
| eval result_a_version_3=50 
| eval result_b_version_3=20 
| fields - _time 
| table test result_a_version_1 result_b_version_1 result_a_version_2 result_b_version_2 result_a_version_3 result_b_version_3
| foreach result_*_version_* 
    [ eval total_of_version_<<MATCHSEG2>>=if(isnotnull(total_of_version_<<MATCHSEG2>>),total_of_version_<<MATCHSEG2>>+'<<FIELD>>','<<FIELD>>') ] 

Thanks

D2SI
Communicator

Seems to be what I was after using MATCHSEG2, thanks a lot !

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

Glad to help you.

Happy Splunking

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!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...