<?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 how to make  loop in splunk query in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/how-to-make-loop-in-splunk-query/m-p/451752#M171371</link>
    <description>&lt;P&gt;My final purpose is factor1 grouping.&lt;BR /&gt;
I want somebody see before / after search result and  code.&lt;/P&gt;

&lt;P&gt;how to make  for loop in splunk query? &lt;/P&gt;

&lt;P&gt;*befor search result&lt;BR /&gt;
factor1 |   factor1_hierarchy_flag  |   factor1_hierarchy_level |   factor1_min |   factor1_max&lt;BR /&gt;
num1    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num2    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num3    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num4    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num5    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num6    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num7    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num8    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num9    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num10   |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
… |   … |   … |   … |   …&lt;/P&gt;

&lt;P&gt;*wanted query&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;factor1_hierarchy_level = 100
factor1_refference_value = 'one of all factor1 number'    

  for(i=1, i&amp;lt;=factor1_hierarchy_level, i=i+1)
 {
     factor1_prev=factor1_min+factor1_hierarchy_flag*(i-1)
     factor1_next=factor1_min+factor1_hierarchy_flag*(i)

     case(factor1_prev&amp;lt;factor1_refference_value&amp;lt;factor1_next)
     factor1_grouping=i
     case(factor1_pv&amp;gt;factor1_max)
     return 0
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;*after search result&lt;BR /&gt;
factor1 |   factor1_hierarchy_flag  |   factor1_hierarchy_level |   factor1_min |   factor1_max |   factor1_grouping&lt;BR /&gt;
num1    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num2    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num3    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num4    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num5    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num6    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num7    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num8    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num9    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num10   |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
… |   … |   … |   … |   … |   …&lt;/P&gt;</description>
    <pubDate>Wed, 30 Sep 2020 00:28:27 GMT</pubDate>
    <dc:creator>leejaeyong</dc:creator>
    <dc:date>2020-09-30T00:28:27Z</dc:date>
    <item>
      <title>how to make  loop in splunk query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-make-loop-in-splunk-query/m-p/451752#M171371</link>
      <description>&lt;P&gt;My final purpose is factor1 grouping.&lt;BR /&gt;
I want somebody see before / after search result and  code.&lt;/P&gt;

&lt;P&gt;how to make  for loop in splunk query? &lt;/P&gt;

&lt;P&gt;*befor search result&lt;BR /&gt;
factor1 |   factor1_hierarchy_flag  |   factor1_hierarchy_level |   factor1_min |   factor1_max&lt;BR /&gt;
num1    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num2    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num3    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num4    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num5    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num6    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num7    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num8    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num9    |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
num10   |   NumA    |   100 |   NumB    |   NumC&lt;BR /&gt;
… |   … |   … |   … |   …&lt;/P&gt;

&lt;P&gt;*wanted query&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;factor1_hierarchy_level = 100
factor1_refference_value = 'one of all factor1 number'    

  for(i=1, i&amp;lt;=factor1_hierarchy_level, i=i+1)
 {
     factor1_prev=factor1_min+factor1_hierarchy_flag*(i-1)
     factor1_next=factor1_min+factor1_hierarchy_flag*(i)

     case(factor1_prev&amp;lt;factor1_refference_value&amp;lt;factor1_next)
     factor1_grouping=i
     case(factor1_pv&amp;gt;factor1_max)
     return 0
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;*after search result&lt;BR /&gt;
factor1 |   factor1_hierarchy_flag  |   factor1_hierarchy_level |   factor1_min |   factor1_max |   factor1_grouping&lt;BR /&gt;
num1    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num2    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num3    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num4    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num5    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num6    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num7    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num8    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num9    |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
num10   |   NumA    |   100 |   NumB    |   NumC    |   one of number from 1 to 100&lt;BR /&gt;
… |   … |   … |   … |   … |   …&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 00:28:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-make-loop-in-splunk-query/m-p/451752#M171371</guid>
      <dc:creator>leejaeyong</dc:creator>
      <dc:date>2020-09-30T00:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to make  loop in splunk query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-make-loop-in-splunk-query/m-p/451753#M171372</link>
      <description>&lt;P&gt;So when wanting to loop in Splunk, I typically try to take advantage of the fact that splunk is already looping through my events.  But sometimes to do that, you have to use spl to add/remove/modify events in order to have the right result set to then take advantage of that inherent looping.  It took me a while to get it, but i really think of spl as more like jiu jitsu to programming's boxing...if that makes any sense.&lt;/P&gt;

&lt;P&gt;So in this case, i would probably do something like this:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;For each event, create a multi-value field with numbers ranging from 1 to 100&lt;/LI&gt;
&lt;LI&gt;Then i would mvexpand that field - so now each original event is actually 100 events - the only difference between them is the new number field (your i iterator)&lt;/LI&gt;
&lt;LI&gt;So now splunk will inherently loop for me&lt;/LI&gt;
&lt;LI&gt;So i can calculate whatever that is that's inside your loop, and drop it in a new field for that event&lt;/LI&gt;
&lt;LI&gt;Then, i can filter my events similarly to your case statements&lt;/LI&gt;
&lt;LI&gt;and then when done, should similar to your desired output i think&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;Maybe something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;your search&amp;gt;
| eval i = mvrange(1,100)
| mvexpand i
| eval reference = 50, prev=factor1_min+factor1_hierarchy_flag*(i-1), next=factor1_min+factor1_hierarchy_flag*(i)
| eval keep = case(next &amp;gt; reference AND prev &amp;lt; reference, 1)
| where keep=1
| fields - keep
| rename i AS factor1_grouping
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Honestly, i have no idea what you're actually doing in your calculations or what that reference value is, and so not sure if this search produces the expected results.  But hopefully it at least gives you an idea of how i would handle the looping part of the question.  It's all about manipulating your result set with SPL until you have something that will work for splunk's inherent looping.  &lt;/P&gt;</description>
      <pubDate>Thu, 09 May 2019 23:37:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-make-loop-in-splunk-query/m-p/451753#M171372</guid>
      <dc:creator>maciep</dc:creator>
      <dc:date>2019-05-09T23:37:01Z</dc:date>
    </item>
  </channel>
</rss>

