Splunk Search

skip step in foreach

spisiakmi
Contributor

Hi, any help, please?

Here is the code

| makeresults
| eval tmp_1=1
| eval tmp_2=""
| eval tmp_3=3
| eval tmp=""
| foreach tmp_*
[| eval tmp = tmp ."|".if(isnotnull(<<FIELD>>),<<FIELD>>,"")]
| eval tmp=substr(tmp, 2)
| table tmp

Output is: tmp=1||3

I would like to have output tmp="1|3", it means how to skip the step in foreach, if the <<FIELD>> is null?

Labels (1)
0 Karma
1 Solution

livehybrid
SplunkTrust
SplunkTrust

Hi @spisiakmi 

I dont think your isnotnull is matchnig as you expect on "", try this:

| makeresults
| eval tmp_1=1
| eval tmp_2=""
| eval tmp_3=3
| eval tmp=""
| foreach tmp_*
[| eval tmp = tmp.if(<<FIELD>>!="",<<FIELD>>."|","")]
| eval tmp=trim(tmp,"|")
| table tmp

 

View solution in original post

livehybrid
SplunkTrust
SplunkTrust

Hi @spisiakmi 

I dont think your isnotnull is matchnig as you expect on "", try this:

| makeresults
| eval tmp_1=1
| eval tmp_2=""
| eval tmp_3=3
| eval tmp=""
| foreach tmp_*
[| eval tmp = tmp.if(<<FIELD>>!="",<<FIELD>>."|","")]
| eval tmp=trim(tmp,"|")
| table tmp

 

spisiakmi
Contributor

livehybrid thank you very much, simple, easy, fast. Very good.

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @spisiakmi ,

please try this:

| makeresults
| eval tmp_1=1
| eval tmp_2=""
| eval tmp_3=3
| eval tmp=""
| foreach tmp_*
[| eval tmp = tmp ."|".if(isnotnull(<<FIELD>>),<<FIELD>>,"")]
| eval tmp=substr(tmp, 2)
| rex field=tmp mode=sed "s/\|+/|/g"
| table tmp

Ciao.

Giuseppe

 

spisiakmi
Contributor

Hey gcusello, thank you very much.

0 Karma
Get Updates on the Splunk Community!

.conf25 Community Recap

Hello Splunkers, And just like that, .conf25 is in the books! What an incredible few days — full of learning, ...

Splunk App Developers | .conf25 Recap & What’s Next

If you stopped by the Builder Bar at .conf25 this year, thank you! The retro tech beer garden vibes were ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...