<?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 with subsearch in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321386#M96001</link>
    <description>&lt;P&gt;@kennethyeung, I think you intend to run the &lt;CODE&gt;map&lt;/CODE&gt; command not foreach. &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;If it does not work for you, please re-post your existing search with &lt;CODE&gt;code button (101010)&lt;/CODE&gt; so that special characters do not escape.&lt;/P&gt;</description>
    <pubDate>Wed, 06 Dec 2017 02:27:44 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2017-12-06T02:27:44Z</dc:date>
    <item>
      <title>foreach with subsearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321385#M96000</link>
      <description>&lt;P&gt;i search in splunk , seem that foreach cannot pass the '&amp;gt;FIELD&amp;lt;' into Subsearch , i search that have to use map command&lt;BR /&gt;
i have below search , could someone help me change to map search?&lt;/P&gt;

&lt;P&gt;index=test code IN (1,3) &lt;BR /&gt;
|  foreach 1 3 &lt;BR /&gt;
    [ eval code&amp;lt;&lt;FIELD&gt;&amp;gt;=  [search index=test code=&amp;lt;&lt;FILED&gt;&amp;gt; |  eval c= price|return $c ]]&lt;/FILED&gt;&lt;/FIELD&gt;&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2017 01:26:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321385#M96000</guid>
      <dc:creator>kennethyeung</dc:creator>
      <dc:date>2017-12-06T01:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: foreach with subsearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321386#M96001</link>
      <description>&lt;P&gt;@kennethyeung, I think you intend to run the &lt;CODE&gt;map&lt;/CODE&gt; command not foreach. &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;If it does not work for you, please re-post your existing search with &lt;CODE&gt;code button (101010)&lt;/CODE&gt; so that special characters do not escape.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2017 02:27:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321386#M96001</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-12-06T02:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: foreach with subsearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321387#M96002</link>
      <description>&lt;P&gt;Hello Niketnilay,&lt;/P&gt;

&lt;P&gt;I have some data like below&lt;/P&gt;

&lt;P&gt;date, code, price&lt;BR /&gt;
20171108,A,1&lt;BR /&gt;
20171109,A,1.5&lt;BR /&gt;
20171110,A,2&lt;BR /&gt;
20171108,B,10&lt;BR /&gt;
20171109,B,20&lt;BR /&gt;
20171110,B,5&lt;/P&gt;

&lt;P&gt;want to get result like below&lt;BR /&gt;
date, codeA, codeB&lt;BR /&gt;
20171108,,0,0&lt;BR /&gt;
20171109,,50,200&lt;BR /&gt;
20171110,,200,-50&lt;/P&gt;

&lt;P&gt;my idea is &lt;BR /&gt;
index=test code IN (1,3) &lt;BR /&gt;
| foreach 1 3 &lt;BR /&gt;
[ eval code&amp;lt;&amp;lt;101010)&amp;gt; &amp;gt; = [search index=test code=&amp;lt;&amp;lt;101010)&amp;gt; &amp;gt; | tail 1 | eval c= price|return $c ]]&lt;BR /&gt;
| foreach code_* [eval p_code_&amp;lt;&amp;gt;=close/close_&amp;lt;&amp;gt;]&lt;BR /&gt;
| ... chart sum(p_code) by date, code&lt;/P&gt;

&lt;P&gt;I need the subsearch to search the oldest record and return the price as the base.&lt;/P&gt;

&lt;P&gt;101010=FIELD&lt;/P&gt;

&lt;P&gt;Thank your for your help&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 17:08:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321387#M96002</guid>
      <dc:creator>kennethyeung</dc:creator>
      <dc:date>2020-09-29T17:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: foreach with subsearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321388#M96003</link>
      <description>&lt;P&gt;Thanks, i use join the solve my question, thank your for your help,&lt;BR /&gt;
I am newibe in splunk, used to think as programmer.&lt;/P&gt;

&lt;P&gt;index=test code IN (A,B) &lt;/P&gt;

&lt;P&gt;|  join code &lt;BR /&gt;
    [search index=test &lt;BR /&gt;
    |  tail &lt;BR /&gt;
        [search |eval code_count  = mvcount(split("A,B",","))&lt;BR /&gt;
        |  return $code_count]&lt;BR /&gt;
    | table code, close&lt;BR /&gt;
    |  rename close as baseclose]&lt;BR /&gt;
| eval percent=(close-baseclose)/baseclose*100&lt;BR /&gt;
| chart sum(percent) by date,code&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 17:09:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321388#M96003</guid>
      <dc:creator>kennethyeung</dc:creator>
      <dc:date>2020-09-29T17:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: foreach with subsearch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321389#M96004</link>
      <description>&lt;P&gt;@kennethyeung, your query and use case is still not clear. The code button is in Splunk Answers Text Box when you type in.&lt;/P&gt;

&lt;P&gt;How you are calculating percent? Can you show example with data? What is the &lt;CODE&gt;close&lt;/CODE&gt; field(it has not been mentioned in your prior posts)?&lt;/P&gt;

&lt;P&gt;Most likely you do not need join. You can check out &lt;CODE&gt;eventstats&lt;/CODE&gt; to calculate stats like &lt;CODE&gt;sum(price) as Total by code&lt;/CODE&gt; and persist the same on events. Then you can calculate percent later.&lt;/P&gt;

&lt;P&gt;Following is a run anywhere search that cooks up data as per your question. Commands till &lt;CODE&gt;| table date code price&lt;/CODE&gt;, generate dummy data.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval data="20171108,A,1;20171109,A,1.5;20171110,A,2;20171108,B,10;20171109,B,20;20171110,B,5"
| makemv data delim=";"
| mvexpand data
| eval data=split(data,",")
| eval date=mvindex(data,0), code=mvindex(data,1), price=mvindex(data,2)
| table date code price
| eventstats sum(price) as Total by code
| chart sum(price)  as Price values(Total) as Total by date code
| foreach "Price: *" [ eval "Percent: &amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;"= round(('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'/'Total: &amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;')*100,1)]
| table date Percent*
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;PS: I am not sure on your logic for Calculation of Percent, but hopefully this should guide you.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2017 07:01:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-with-subsearch/m-p/321389#M96004</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-12-06T07:01:47Z</dc:date>
    </item>
  </channel>
</rss>

