<?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: Foreach in Multisearch in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417253#M120075</link>
    <description>&lt;P&gt;That is more than your original query produced.  What exactly do you want in the results?&lt;/P&gt;</description>
    <pubDate>Fri, 01 Mar 2019 13:39:17 GMT</pubDate>
    <dc:creator>richgalloway</dc:creator>
    <dc:date>2019-03-01T13:39:17Z</dc:date>
    <item>
      <title>Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417244#M120066</link>
      <description>&lt;P&gt;Hi, I wonder whether someone can help me please.&lt;/P&gt;

&lt;P&gt;I've put together the query below using the foreach command, which, although I've read a lot of posts, I've not really used, or if truth be known understood a great deal.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| multisearch
[ search `gateway_wmf(ClientRequest)` path=*vat*]
[ search `wso2_wmf(RequestCompleted)` "request.detail.apiContext"=*test]
| eval RequestID=coalesce('request.tags.X-Request-ID','requestID')
| dedup eventId
| rename request.detail.applicationProductionClientId as ClientID response.detail.statusCode AS statusCode
| foreach clientHeaders.test* [eval header='&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']
| stats count(header) by RequestID 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The query runs, but there is no new field called "header" created and hence I don't receive my stats count at the end of the query.&lt;/P&gt;

&lt;P&gt;Could someone perhaps have a look a this please and offer some guidance on where I've gone wrong and a brief explanation of the 'foreach' command.&lt;/P&gt;

&lt;P&gt;Many thanks and kind regards&lt;/P&gt;

&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 07:15:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417244#M120066</guid>
      <dc:creator>IRHM73</dc:creator>
      <dc:date>2019-02-28T07:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417245#M120067</link>
      <description>&lt;P&gt;The &lt;CODE&gt;foreach&lt;/CODE&gt; command repeats the commands in its subsearch for each field (not value) that matches the first argument.  In your query, the &lt;CODE&gt;eval&lt;/CODE&gt; command will be invoked for all of the fields with names beginning with 'clientHeaders.test'.  If there are no such fields then the &lt;CODE&gt;eval&lt;/CODE&gt; is not executed at all.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 13:46:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417245#M120067</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2019-02-28T13:46:23Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417246#M120068</link>
      <description>&lt;P&gt;Hi @richgalloway, thank you for coming back to me with this.&lt;/P&gt;

&lt;P&gt;I've managed to put a solution together as follows:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| multisearch
[ search `gateway_wmf(ClientRequest)` path=*test*]
[ search `wso2_wmf(RequestCompleted)` "request.detail.apiContext"=*test]
| eval RequestID=coalesce('request.tags.X-Request-ID','requestID')
| eval header=""
| foreach clientHeaders.test* [eval header='&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']
| bucket span=10s _time
| stats count(header) by RequestID _time header
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem I have, and this is through lack of knowledge, I didn't realise that it extracted the field value rather than the fieldname.&lt;/P&gt;

&lt;P&gt;Could you tell me please is there a way to count the field headers rather than the values?&lt;/P&gt;

&lt;P&gt;Many thanks and kind regards&lt;/P&gt;

&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 14:03:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417246#M120068</guid>
      <dc:creator>IRHM73</dc:creator>
      <dc:date>2019-02-28T14:03:43Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417247#M120069</link>
      <description>&lt;P&gt;It's not very elegant, but try this.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| multisearch
[ search `gateway_wmf(ClientRequest)` path=*test*]
[ search `wso2_wmf(RequestCompleted)` "request.detail.apiContext"=*test]
| eval RequestID=coalesce('request.tags.X-Request-ID','requestID')
| eval header=""
| foreach clientHeaders.test* [eval header=header+test&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;]
| bucket span=10s _time
| stats count(header) by RequestID _time header
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Feb 2019 16:05:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417247#M120069</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2019-02-28T16:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417248#M120070</link>
      <description>&lt;P&gt;Hi @richgalloway.&lt;/P&gt;

&lt;P&gt;Thank you for coming back to me with this. May I just check, in this &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;[eval header=header+test&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;]&lt;/CODE&gt;. &lt;/P&gt;

&lt;P&gt;The test element. Where is this taken from?&lt;/P&gt;

&lt;P&gt;Many thanks and kind regards&lt;/P&gt;

&lt;P&gt;Chris &lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 07:19:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417248#M120070</guid>
      <dc:creator>IRHM73</dc:creator>
      <dc:date>2019-03-01T07:19:32Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417249#M120071</link>
      <description>&lt;P&gt;If there is no header field on the search result I guess there is no fields clientHeaders.test with a suffix.&lt;/P&gt;

&lt;P&gt;Check this dummy example, where only one field called header is created, which seems to not have much logic when framing this with the foreach command&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| makeresults | eval clientHeaders.test1=1, clientHeaders.test2=2 | foreach clientHeaders.test* [eval header='&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;If you want a field header_ for each of the clientHeaders.test, you can use&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| makeresults | eval clientHeaders.test1=1, clientHeaders.test2=2 | foreach clientHeaders.test* [eval header_&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;=&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;In my opinion it doesn't seem to be what you want to achieve from what I get from your query. Would you put here an anonymyzed example of existing fields at this point:&lt;/P&gt;

&lt;P&gt;| multisearch&lt;BR /&gt;
[ search gateway_wmf(ClientRequest) path=vat]&lt;BR /&gt;
[ search wso2_wmf(RequestCompleted) "request.detail.apiContext"=*test]&lt;BR /&gt;
| eval RequestID=coalesce('request.tags.X-Request-ID','requestID')&lt;BR /&gt;
| dedup eventId&lt;BR /&gt;
| rename request.detail.applicationProductionClientId as ClientID response.detail.statusCode AS statusCode&lt;/P&gt;

&lt;P&gt;And get us what you want to get in the end?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 23:32:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417249#M120071</guid>
      <dc:creator>tiagofbmm</dc:creator>
      <dc:date>2020-09-29T23:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417250#M120072</link>
      <description>&lt;P&gt;It comes from the query in your question.  You had &lt;CODE&gt;eval header=&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;&lt;/CODE&gt; where FIELD is clientHeaders.test* so I used MATCHSTR to get the &lt;CODE&gt;*&lt;/CODE&gt; part and inserted 'test' manually.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 12:40:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417250#M120072</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2019-03-01T12:40:44Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417251#M120073</link>
      <description>&lt;P&gt;Hi @tiagofbmm . Thank you for coming back to me with this.&lt;/P&gt;

&lt;P&gt;So some examples of the fields are:&lt;/P&gt;

&lt;P&gt;clientHeaders.test-client-colour-depth&lt;BR /&gt;
clientHeaders.test-client-device-id&lt;BR /&gt;
clientHeaders.test-client-device-ip&lt;BR /&gt;
clientHeaders.test-test-scenario&lt;/P&gt;

&lt;P&gt;But this list is not exhaustive. It will increase but I would like to 'futureproof' the query so I don't need to constantly update when new fields appear with this naming convention, if possible please.&lt;/P&gt;

&lt;P&gt;And for the columns, I'd like if possible to be as follows:&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;RequestID&lt;/STRONG&gt;       &lt;STRONG&gt;Time&lt;/STRONG&gt;     &lt;STRONG&gt;Multiple columns where fields start with clientHeaders.test&lt;/STRONG&gt;&lt;BR /&gt;
1234           15/05      Count for each column heading&lt;/P&gt;

&lt;P&gt;I hope this makes sense and once again many thanks.&lt;/P&gt;

&lt;P&gt;Kind Regards&lt;/P&gt;

&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 13:28:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417251#M120073</guid>
      <dc:creator>IRHM73</dc:creator>
      <dc:date>2019-03-01T13:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417252#M120074</link>
      <description>&lt;P&gt;Hi @richgalloway. Thank you. I thought that was the case.&lt;/P&gt;

&lt;P&gt;Unfortunately it only gives me two columns, 1 called header and the the other count(header), in addition to the time and RequestID.&lt;/P&gt;

&lt;P&gt;Many thanks and regards&lt;/P&gt;

&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 13:36:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417252#M120074</guid>
      <dc:creator>IRHM73</dc:creator>
      <dc:date>2019-03-01T13:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417253#M120075</link>
      <description>&lt;P&gt;That is more than your original query produced.  What exactly do you want in the results?&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 13:39:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417253#M120075</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2019-03-01T13:39:17Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417254#M120076</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;

&lt;P&gt;Running them side by side, the results are table layout are the same from what I can see.&lt;/P&gt;

&lt;P&gt;Here is the layout I'd like if possible please:&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;RequestID Time Multiple columns where fields start with clientHeaders.test&lt;/STRONG&gt;&lt;BR /&gt;
1234 15/05 Count for each column heading&lt;/P&gt;

&lt;P&gt;These are examples of the fieldnames:&lt;BR /&gt;
clientHeaders.test-client-device-id&lt;BR /&gt;
clientHeaders.test-client-device-ip&lt;BR /&gt;
clientHeaders.test-test-scenario&lt;/P&gt;

&lt;P&gt;But this list is not exhaustive. It will increase but I would like to 'futureproof' the query so I don't need to constantly update when new fields appear with this naming convention, if possible please.&lt;/P&gt;

&lt;P&gt;Many thanks and regards&lt;/P&gt;

&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 14:23:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417254#M120076</guid>
      <dc:creator>IRHM73</dc:creator>
      <dc:date>2019-03-01T14:23:58Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417255#M120077</link>
      <description>&lt;P&gt;Try this.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| multisearch
[ search `gateway_wmf(ClientRequest)` path=*test*]
[ search `wso2_wmf(RequestCompleted)` "request.detail.apiContext"=*test]
| eval RequestID=coalesce('request.tags.X-Request-ID','requestID')
| eval header=""
| foreach clientHeaders.test* [eval header=header+test&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;]
| table RequestID _time header
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Mar 2019 12:42:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417255#M120077</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2019-03-02T12:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417256#M120078</link>
      <description>&lt;P&gt;Hi @richgalloway. Thank you for posting this.&lt;/P&gt;

&lt;P&gt;As you would expect the table has the 3 columns, but unfortunately the "header" column is blank.&lt;/P&gt;

&lt;P&gt;Many thanks and kind regards&lt;/P&gt;

&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Sat, 02 Mar 2019 18:15:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417256#M120078</guid>
      <dc:creator>IRHM73</dc:creator>
      <dc:date>2019-03-02T18:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417257#M120079</link>
      <description>&lt;P&gt;What do you get when you change the &lt;CODE&gt;table&lt;/CODE&gt; command to &lt;CODE&gt;| table RequestID _time header clientHeaders.test*&lt;/CODE&gt;?&lt;/P&gt;</description>
      <pubDate>Sat, 02 Mar 2019 18:22:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417257#M120079</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2019-03-02T18:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: Foreach in Multisearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417258#M120080</link>
      <description>&lt;P&gt;Hi @richgalloway . That makes a huge difference. &lt;/P&gt;

&lt;P&gt;I now have the columns I want.....it's great.&lt;/P&gt;

&lt;P&gt;I just now need to work out had to incorporate the counts but I think I can work with that.&lt;/P&gt;

&lt;P&gt;Thank you so much for all your time and trouble, it's sincerely appreciated.&lt;/P&gt;

&lt;P&gt;Kind Regards&lt;/P&gt;

&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Sat, 02 Mar 2019 18:28:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Foreach-in-Multisearch/m-p/417258#M120080</guid>
      <dc:creator>IRHM73</dc:creator>
      <dc:date>2019-03-02T18:28:36Z</dc:date>
    </item>
  </channel>
</rss>

