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
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...