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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...