<?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 Reduce a result set using the foreach splunk search command in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Reduce-a-result-set-using-the-foreach-splunk-search-command/m-p/199018#M57612</link>
    <description>&lt;P&gt;The result of a splunk query is the following:&lt;/P&gt;

&lt;P&gt;Result set 1:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;method  success failures    Over_method1    Over_method2    Over_method3
method1 0   73               3                 0            0
method2 196 0                0                 2            0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I need to reduce this result set as follow:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;method  success failures    Over
method1    0    73            3
method2   196   0             2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I tried using the search command &lt;EM&gt;foreach&lt;/EM&gt; but no success.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk search to get result set 1|
foreach Over_* [eval Over=Over+&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;]|
table method success failures Over 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;OR &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk to get result set 1|
foreach Over_* [eval Over=Over+'&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']|
table method success failures Over 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This could be done with the following query:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk search to get result set 1|
=if(Over_method1&amp;gt;0,Over_method1,if(Over_method2&amp;gt;0,Over_method2,0))|
table method success failures Over 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, A simpler way is to use the foreach function. To make it work the mapping variable needs to be initialized as presented in below answer. If not the result set will not be reduced correctly. So the final query is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk to get result set 1|eval Over=0|
foreach Over_* [eval Over=Over+'&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']|
table method success failures Over 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Lp&lt;/P&gt;</description>
    <pubDate>Mon, 16 Jun 2014 18:49:08 GMT</pubDate>
    <dc:creator>lpolo</dc:creator>
    <dc:date>2014-06-16T18:49:08Z</dc:date>
    <item>
      <title>Reduce a result set using the foreach splunk search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Reduce-a-result-set-using-the-foreach-splunk-search-command/m-p/199018#M57612</link>
      <description>&lt;P&gt;The result of a splunk query is the following:&lt;/P&gt;

&lt;P&gt;Result set 1:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;method  success failures    Over_method1    Over_method2    Over_method3
method1 0   73               3                 0            0
method2 196 0                0                 2            0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I need to reduce this result set as follow:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;method  success failures    Over
method1    0    73            3
method2   196   0             2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I tried using the search command &lt;EM&gt;foreach&lt;/EM&gt; but no success.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk search to get result set 1|
foreach Over_* [eval Over=Over+&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;]|
table method success failures Over 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;OR &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk to get result set 1|
foreach Over_* [eval Over=Over+'&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']|
table method success failures Over 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This could be done with the following query:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk search to get result set 1|
=if(Over_method1&amp;gt;0,Over_method1,if(Over_method2&amp;gt;0,Over_method2,0))|
table method success failures Over 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, A simpler way is to use the foreach function. To make it work the mapping variable needs to be initialized as presented in below answer. If not the result set will not be reduced correctly. So the final query is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk to get result set 1|eval Over=0|
foreach Over_* [eval Over=Over+'&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']|
table method success failures Over 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Lp&lt;/P&gt;</description>
      <pubDate>Mon, 16 Jun 2014 18:49:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Reduce-a-result-set-using-the-foreach-splunk-search-command/m-p/199018#M57612</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2014-06-16T18:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: Reduce a result set using the foreach splunk search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Reduce-a-result-set-using-the-foreach-splunk-search-command/m-p/199019#M57613</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk search to get result set 1| eval Over=0 | foreach Over_* [eval Over=Over + &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;]|table method success failures Over
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Alternative (using untable) &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Splunk search to get result set 1 | untable method key value  | eval key=case(like(key,"Over%"),"Over",1=1,key) | chart sum(value) over method by key
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Jun 2014 20:01:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Reduce-a-result-set-using-the-foreach-splunk-search-command/m-p/199019#M57613</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2014-06-16T20:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: Reduce a result set using the foreach splunk search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Reduce-a-result-set-using-the-foreach-splunk-search-command/m-p/199020#M57614</link>
      <description>&lt;P&gt;Thanks. I made a little change for the eval expression to work, &amp;lt;&lt;FIELD&gt;&amp;gt; needs to be surrounded by single quotes. I updated the original question. &lt;/FIELD&gt;&lt;/P&gt;

&lt;P&gt;|Splunk search to get result set 1|eval Over=0 | foreach Over_* [eval Over=Over+'&amp;lt;&lt;FIELD&gt;&amp;gt;']|table method success failures Over&lt;/FIELD&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jun 2014 11:29:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Reduce-a-result-set-using-the-foreach-splunk-search-command/m-p/199020#M57614</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2014-06-17T11:29:05Z</dc:date>
    </item>
    <item>
      <title>Re: Reduce a result set using the foreach splunk search command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Reduce-a-result-set-using-the-foreach-splunk-search-command/m-p/199021#M57615</link>
      <description>&lt;P&gt;However, After reading the answer presented bellow I made a little change for the eval expression to work, &amp;lt;&lt;FIELD&gt;&amp;gt; needs to be surrounded by single quotes.&lt;/FIELD&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jun 2014 11:32:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Reduce-a-result-set-using-the-foreach-splunk-search-command/m-p/199021#M57615</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2014-06-17T11:32:04Z</dc:date>
    </item>
  </channel>
</rss>

