<?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: How to do a for loop in Splunk query? in Dashboards &amp; Visualizations</title>
    <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458181#M30048</link>
    <description>&lt;P&gt;That "2" on line 3 needs to be a 1 and you need to use mvexpand. But, besides that thank you for this approach! Here is my code (note that it is for a slight variation on the question):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval count=$n$ 
| eval count=mvrange(1,count+1, 1) 
| mvexpand count 
| eval filename=strftime(now()-count*86400,"abc_machines_pings-%Y-%m-%d.csv")
| map maxsearches=28 search="| inputcsv $filename$ | where pingdelay != 10000000  and pingdelay != \"\""
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 27 Jul 2018 20:14:54 GMT</pubDate>
    <dc:creator>nick405060</dc:creator>
    <dc:date>2018-07-27T20:14:54Z</dc:date>
    <item>
      <title>How to do a for loop in Splunk query?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458178#M30045</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;

&lt;P&gt;I have users of my dashboard specify integer N in a dropdown menu. In a query that has results displayed on my dashboard in a table, I'd like to outer join "mycsv_1.csv", "mycsv_2.csv", "mycsv_3.csv," ... "mycsv_N.csv" as part of that query.&lt;/P&gt;

&lt;P&gt;The inputcsv/join syntax is easy for me, but how do I do the iteration?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 20:36:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458178#M30045</guid>
      <dc:creator>nick405060</dc:creator>
      <dc:date>2020-09-29T20:36:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to do a for loop in Splunk query?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458179#M30046</link>
      <description>&lt;P&gt;I was able to implement this in a slightly convoluted way - if anyone can figure out how to do it better please let me know.&lt;/P&gt;

&lt;P&gt;I set up a scripted input that takes in $n$ and copies mycsv_1.csv ... mycsv_n.csv to temp_1.csv ... temp_n.csv. Then, my Splunk query looks like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| script MyPythonScript $n$ | {attemps to outer join temp_1.csv, temp_2.csv, temp_3.csv, ...temp_MAX.csv (I used 20 as MAX), and since the temp files that are between $n$ and MAX were never copied over by the Python script, they don't exist and aren't joined. Only mycsv_1 to mycsv_N.csv are joined}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Python code (the scenario this code addresses is slightly different than the scenario described in the initial question. Using dates in csv filenames instead of integers):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# Takes an integer N and then saves 'C:\Program Files\Splunk\var\run\splunk\ABC_machines_pings-yyyy-mm-dd.csv' files
# (where yyyy-mm-dd is 0...N days before now()) as temp_(0...N).csv files

# Since you can't iterate in a Splunk query (ideally a user would specify N and we could inputcsv ABC_machines_pings_0...N only), I
# instead created this script to just copy and rename files 0...N so that selecting temp_* in a Splunk query will give csv files 0...N only

from datetime import datetime, timedelta
import os
import sys

n = int(sys.argv[1])
dir='C:\\Program Files\\Splunk\\var\\run\\splunk\\'

# Removes former temp_n files
for file in os.listdir(dir):
    if 'temp_' in file:
        os.remove(dir+file)

# Creates new temp_n files
for i in range(n):
    path = dir + 'ABC_machines_pings-' + (datetime.today() - timedelta(days=i)).strftime('%Y-%m-%d') + '.csv'
    os.system('copy \"' + path + '\" \"' + dir + 'temp_' + str(i) + '.csv\"')
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 20:37:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458179#M30046</guid>
      <dc:creator>nick405060</dc:creator>
      <dc:date>2020-09-29T20:37:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to do a for loop in Splunk query?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458180#M30047</link>
      <description>&lt;P&gt;Like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval count=$YourIndexValueTokenNameHere$
| eval filename=mvrange(1,count+1, 1)
| mvexpand filename
| rex field=filename mode=sed "s/(.*)/mycsv_\1.csv/"
| map maxsearches=99 search="| inputlookup $filename$"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can test by running this in your search bar and using &lt;CODE&gt;| eval count = 11&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 23:47:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458180#M30047</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2018-07-26T23:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to do a for loop in Splunk query?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458181#M30048</link>
      <description>&lt;P&gt;That "2" on line 3 needs to be a 1 and you need to use mvexpand. But, besides that thank you for this approach! Here is my code (note that it is for a slight variation on the question):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval count=$n$ 
| eval count=mvrange(1,count+1, 1) 
| mvexpand count 
| eval filename=strftime(now()-count*86400,"abc_machines_pings-%Y-%m-%d.csv")
| map maxsearches=28 search="| inputcsv $filename$ | where pingdelay != 10000000  and pingdelay != \"\""
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Jul 2018 20:14:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458181#M30048</guid>
      <dc:creator>nick405060</dc:creator>
      <dc:date>2018-07-27T20:14:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to do a for loop in Splunk query?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458182#M30049</link>
      <description>&lt;P&gt;Quite right; I pasted wrong version. I re-edited and fixed it.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 21:14:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/How-to-do-a-for-loop-in-Splunk-query/m-p/458182#M30049</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2018-07-27T21:14:37Z</dc:date>
    </item>
  </channel>
</rss>

