<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: What should I use instead of foreach when iterating? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329728#M98164</link>
    <description>&lt;P&gt;Since you only have three values, you won't even really need maxsearches at all. Give it a try without it.&lt;/P&gt;</description>
    <pubDate>Thu, 12 Apr 2018 19:08:52 GMT</pubDate>
    <dc:creator>elliotproebstel</dc:creator>
    <dc:date>2018-04-12T19:08:52Z</dc:date>
    <item>
      <title>What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329717#M98153</link>
      <description>&lt;P&gt;When running the following - &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 1 
| eval total=0 
| eval server1=host1 
| eval server2=host2
| eval server3=host3

| foreach server*

 [ 
   | forwarderquery server=&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; api="/services/server/info" stanza="unix_forwarder"
    contenttype="json" 
| spath path="entry{}" output=entry 
| fields - _raw 
| mvexpand entry 
| spath input=entry 
 ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I get the error message - &lt;BR /&gt;
&lt;EM&gt;Error in 'foreach' command: Search pipeline may not contain non-streaming commands&lt;/EM&gt;&lt;/P&gt;

&lt;P&gt;What can I do instead?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 17:50:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329717#M98153</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2018-04-12T17:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329718#M98154</link>
      <description>&lt;P&gt;How about using &lt;CODE&gt;map&lt;/CODE&gt; instead? Something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval server=host1 
| append 
 [| makeresults
  | eval server=host2 ]
| append
 [| eval server=host3 ] 
 | map maxsearches=3  
  [| forwarderquery server="$server$" api="/services/server/info" stanza="unix_forwarder"
     contenttype="json" 
 | spath path="entry{}" output=entry 
 | fields - _raw 
 | mvexpand entry 
 | spath input=entry ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note that you'll need to change the value of &lt;CODE&gt;maxsearches&lt;/CODE&gt; if you change the number of servers you want to run this over. It's generally not a great idea to use &lt;CODE&gt;map&lt;/CODE&gt; if you can avoid it, but on a limited scale, it's not terrible. Also, if somebody else has an idea that doesn't use &lt;CODE&gt;map&lt;/CODE&gt;, then theirs will almost certainly perform better than mine!&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 18:17:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329718#M98154</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-04-12T18:17:53Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329719#M98155</link>
      <description>&lt;P&gt;@ddrillic, the &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Foreach"&gt;foreach&lt;/A&gt; command is supposed to run template version of eval command. So it is probably not the right option for your use case. Instead of having your outer search result as row with several columns i.e. server1, server2... etc, if you can have single column server with several rows host1, host2.. etc, then you can use &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map"&gt;map&lt;/A&gt; command instead to caryy out inner query for each results.&lt;BR /&gt;
Following is a run anywhere example.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | makeresults 
 | fields - _time
 | eval server="host1,host2,host3"
 | makemv server delim=","
 | mvexpand server
 | eval total=0
 |  map search="| makeresults
                | fields - _time
                | eval serverList=\"$server$\""
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please confirm if this is what you need. PS: map command is restricted by subsearch limitation and &lt;CODE&gt;maxsearches=10&lt;/CODE&gt; is the default option. If your outer search has too many rows, there might be performance issue with the query.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 18:22:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329719#M98155</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-04-12T18:22:41Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329720#M98156</link>
      <description>&lt;P&gt;@elliotproebstel I am too late with my answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 18:23:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329720#M98156</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-04-12T18:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329721#M98157</link>
      <description>&lt;P&gt;Great but there is a complaint - *Search Factory: Unknown search command 's'. * about &lt;CODE&gt;server=$server&lt;/CODE&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 18:38:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329721#M98157</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2018-04-12T18:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329722#M98158</link>
      <description>&lt;P&gt;Fixed! Try it again.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 18:42:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329722#M98158</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-04-12T18:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329723#M98159</link>
      <description>&lt;P&gt;Still upset, screaming ; - )   - *Error in 'map' command: Unable to find saved search 'maxsearches=3'. *&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 18:47:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329723#M98159</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2018-04-12T18:47:25Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329724#M98160</link>
      <description>&lt;P&gt;Hmmm...It seems like maybe the &lt;CODE&gt;maxsearches=3&lt;/CODE&gt; part needs to go at the end? Sorry, I'm air coding at the moment, and clearly not doing that well!&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 18:51:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329724#M98160</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-04-12T18:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329725#M98161</link>
      <description>&lt;P&gt;It's all good ; - ) thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 18:53:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329725#M98161</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2018-04-12T18:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329726#M98162</link>
      <description>&lt;P&gt;@niketnilay, that works!&lt;/P&gt;

&lt;P&gt;I see  -&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/4749i5BF9722C00DFA180/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;What's next? ; -)&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:03:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329726#M98162</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2018-04-12T19:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329727#M98163</link>
      <description>&lt;P&gt;Put your search inside the &lt;CODE&gt;search=...&lt;/CODE&gt; section!&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:08:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329727#M98163</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-04-12T19:08:27Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329728#M98164</link>
      <description>&lt;P&gt;Since you only have three values, you won't even really need maxsearches at all. Give it a try without it.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:08:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329728#M98164</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-04-12T19:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329729#M98165</link>
      <description>&lt;P&gt;Perfect -&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | makeresults 
  | fields - _time
  | eval server="host1,host2,host3"
  | makemv server delim=","
  | mvexpand server
  | eval total=0
  |  map search="| forwarderquery server=$server$ api="/services/server/info" stanza="unix_forwarder"
    contenttype="json" 
  | spath path="entry{}" output=entry 
  | fields - _raw 
  | mvexpand entry 
  | spath input=entry
"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It says 3 results but shows only one event ; -)&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:17:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329729#M98165</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2018-04-12T19:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329730#M98166</link>
      <description>&lt;P&gt;You'll need to escape any double-quotes inside the mapped search. I suspect it's silently failing because of that. You may also need (escaped) double-quotes around the variable: &lt;CODE&gt;server=\"$server$\"&lt;/CODE&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:19:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329730#M98166</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-04-12T19:19:11Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329731#M98167</link>
      <description>&lt;P&gt;I'll try...&lt;/P&gt;

&lt;P&gt;Meanwhile there is another issue -&lt;BR /&gt;
*   [map]: command="forwarderquery", Error : Traceback: 'HTTPSConnectionPool(host='%5C$server%5C$', port=8089): Max retries exceeded with url: /services/server/info *&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:32:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329731#M98167</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2018-04-12T19:32:27Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329732#M98168</link>
      <description>&lt;P&gt;@ddrillic @elliotproebstel, when map command fails it logs the details under Job Inspector with position and error (in most cases ;)). Yes double quote needs to be escaped.&lt;/P&gt;

&lt;P&gt;Refer to @woodcock's answer &lt;A href="https://answers.splunk.com/answers/543009/field-not-fillled-through-eval-in-map.html"&gt;https://answers.splunk.com/answers/543009/field-not-fillled-through-eval-in-map.html&lt;/A&gt; where you can use &lt;CODE&gt;[ ]&lt;/CODE&gt; to use search without having to escape double quotes.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:36:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329732#M98168</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-04-12T19:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329733#M98169</link>
      <description>&lt;P&gt;Yeah, that's why I proposed that search structure in my answer - the original query had a lot of double quotes &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:38:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329733#M98169</guid>
      <dc:creator>elliotproebstel</dc:creator>
      <dc:date>2018-04-12T19:38:10Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329734#M98170</link>
      <description>&lt;P&gt;@ddrillic, I hope you are currently trying query like the following , you also need to escape forward-slashes. Consider map command search string the same way as running a Regular Expression. So, escape all characters with their literal value using backslash i.e. &lt;CODE&gt;\"&lt;/CODE&gt;, &lt;CODE&gt;\/&lt;/CODE&gt; etc as needed.&lt;/P&gt;

&lt;P&gt;Just FYI &lt;CODE&gt;%5C&lt;/CODE&gt; is HTML encoded character for Backslash &lt;CODE&gt;\&lt;/CODE&gt;. So seems like you have missed double quotes in &lt;CODE&gt;$server$&lt;/CODE&gt; i.e. &lt;CODE&gt;\"$server$\"&lt;/CODE&gt; &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| fields - _time 
| eval server="host1,host2,host3" 
| makemv server delim="," 
| mvexpand server 
| eval total=0 
| map search="| forwarderquery server=\"$server$\" api=\"\/services\/server\/info\" stanza=\"unix_forwarder\"
     contenttype=\"json\" 
   | spath path=\"entry{}\" output=entry 
   | fields - _raw 
   | mvexpand entry 
   | spath input=entry"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Further more seems like the error is from the service itself so it is being hit but with wrong value. (I am assuming this is what is happening)&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:50:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329734#M98170</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-04-12T19:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: What should I use instead of foreach when iterating?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329735#M98171</link>
      <description>&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt; For no reasons, I actually prefer using double quotes and escaping all the values using backslash even though query looks much more complicated. &lt;/P&gt;

&lt;P&gt;I feel @ddrillic is almost there as the error seems to be from service rather than map command. Hope he makes it through... Its too late for me now.. Anyways he is in good hands &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 19:55:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-should-I-use-instead-of-foreach-when-iterating/m-p/329735#M98171</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-04-12T19:55:49Z</dc:date>
    </item>
  </channel>
</rss>

