<?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: Using saved search as a &amp;quot;variable&amp;quot; in Reporting</title>
    <link>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314401#M5757</link>
    <description>&lt;P&gt;"Saved search" is a technical term, and is not what you want here.  &lt;/P&gt;

&lt;P&gt;There are a lot of options to go about doing something like this.  &lt;/P&gt;

&lt;P&gt;One fairly trivial one is to run these two in order...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;host=*blah "etc" 
| stats count(host) as mycount 
| table mycount 
| outputcsv mycount.csv

search "etc2" 
| stats count(host) as hostCount 
| append [| inputcsv mycount.csv ]
| stats sum(*) as * 
| eval diff = mycount / hostCount 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;With simple searches like those, it is pretty easy to then combine them into a single search like this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search "etc2" 
| stats count(host) as hostCount 
| append 
    [| search host=*blah "etc" 
     | stats count(host) as mycount 
     | table mycount ] 
| stats sum(*) as * 
| eval diff = mycount / hostCount 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 06 Apr 2017 20:28:04 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2017-04-06T20:28:04Z</dc:date>
    <item>
      <title>Using saved search as a "variable"</title>
      <link>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314400#M5756</link>
      <description>&lt;P&gt;I have a somewhat complex query that I am trying to execute. Essentially what I would like to do is use a saved search as a "variable" of sorts for another search. &lt;/P&gt;

&lt;P&gt;The saved search would be something along the lines of:&lt;BR /&gt;
&lt;CODE&gt;host=*blah "etc" | stats count(host)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;From there, I would think I could use the result of that saved search as a variable for another search, where math is being performed. So, what I envision the other to be:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;search "etc2" | stats count(host) as hostCount| eval diff =  savedSearch / hostCount &lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I've search around to see if this is possible, but I didn't find an conclusive results. &lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2017 16:47:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314400#M5756</guid>
      <dc:creator>ajh11g</dc:creator>
      <dc:date>2017-04-06T16:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: Using saved search as a "variable"</title>
      <link>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314401#M5757</link>
      <description>&lt;P&gt;"Saved search" is a technical term, and is not what you want here.  &lt;/P&gt;

&lt;P&gt;There are a lot of options to go about doing something like this.  &lt;/P&gt;

&lt;P&gt;One fairly trivial one is to run these two in order...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;host=*blah "etc" 
| stats count(host) as mycount 
| table mycount 
| outputcsv mycount.csv

search "etc2" 
| stats count(host) as hostCount 
| append [| inputcsv mycount.csv ]
| stats sum(*) as * 
| eval diff = mycount / hostCount 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;With simple searches like those, it is pretty easy to then combine them into a single search like this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search "etc2" 
| stats count(host) as hostCount 
| append 
    [| search host=*blah "etc" 
     | stats count(host) as mycount 
     | table mycount ] 
| stats sum(*) as * 
| eval diff = mycount / hostCount 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Apr 2017 20:28:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314401#M5757</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-04-06T20:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using saved search as a "variable"</title>
      <link>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314402#M5758</link>
      <description>&lt;P&gt;Yes. Here's an example of using the results from one search in the eval of another search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal sourcetype=splunkd 
| stats dc(splunk_server) as firstcount 
| map search="search index=_internal sourcetype=splunkd | stats dc(host) as secondcount | eval diff=secondcount-$firstcount$"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You should be able to adapt that to your use case pretty easily. Also, check out: &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map"&gt;http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Apr 2017 22:53:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314402#M5758</guid>
      <dc:creator>masonmorales</dc:creator>
      <dc:date>2017-04-06T22:53:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using saved search as a "variable"</title>
      <link>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314403#M5759</link>
      <description>&lt;P&gt;Believe it or not, there is a command called &lt;CODE&gt;savedsearch&lt;/CODE&gt; which allows you to templatize a saved search with tokens set from another search, exactly like how you templatize a dashboard panel with tokens set from the &lt;CODE&gt;fieldset&lt;/CODE&gt; area.&lt;/P&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/6.5.2/SearchReference/Savedsearch"&gt;http://docs.splunk.com/Documentation/Splunk/6.5.2/SearchReference/Savedsearch&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;It works like this:  You save this search as &lt;CODE&gt;MyTemplatizedHostSearch&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=foo sourcetype=bar host=$my_host$
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then, you call this from another search like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|savedsearch MyTemplatizedHostSearch my_host="MyHostValue"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The search that is run will be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    index=foo sourcetype=bar host="MyHostValue"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Apr 2017 19:07:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314403#M5759</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-04-08T19:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using saved search as a "variable"</title>
      <link>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314404#M5760</link>
      <description>&lt;P&gt;If you saved this as &lt;CODE&gt;MySavedSearch&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;host=*blah "etc" | stats count(host) AS hostCount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then you can do this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search "etc2" | stats count(host) as hostCount| eval diff = [| savedsearch MySavedSearch | return $hostCount] / hostCount 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Apr 2017 19:10:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314404#M5760</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-04-08T19:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using saved search as a "variable"</title>
      <link>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314405#M5761</link>
      <description>&lt;P&gt;Also, if you schedule &lt;CODE&gt;MySavedSearch&lt;/CODE&gt;, you could also use &lt;CODE&gt;loadjob&lt;/CODE&gt; to load the results of the previous run (instead of re-running it ad-hoc).&lt;/P&gt;</description>
      <pubDate>Sat, 08 Apr 2017 19:11:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314405#M5761</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-04-08T19:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using saved search as a "variable"</title>
      <link>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314406#M5762</link>
      <description>&lt;P&gt;Thanks! This did exactly what I was looking for.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 18:59:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Reporting/Using-saved-search-as-a-quot-variable-quot/m-p/314406#M5762</guid>
      <dc:creator>ajh11g</dc:creator>
      <dc:date>2017-04-10T18:59:18Z</dc:date>
    </item>
  </channel>
</rss>

