<?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 Search/Macro using a variable as a condition in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212561#M62262</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;

&lt;P&gt;I am trying to use a variable as a search condition based on input in a text box. In order to make it simpler for users, I want them to be able to enter as many potential search strings as possible.&lt;/P&gt;

&lt;P&gt;Textbox details on the dashboard:&lt;BR /&gt;
&lt;CODE&gt;token: jobname&lt;BR /&gt;
default: *&lt;BR /&gt;
token prefix: job=&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Example input: "string1 string2 string3"&lt;/P&gt;

&lt;P&gt;I then put this through an eval to get a search condition to run:&lt;BR /&gt;
&lt;CODE&gt;eventtype=my_eventtype | rex field="some regex (?moreregex)" | eval jobname="$jobname$" | eval jobsearch=replace(jobname, " ", " OR job=")&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;This will mean that the variable jobsearch will be:&lt;BR /&gt;
&lt;CODE&gt;"job=string1 OR job=string2 OR job=string3"&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;How then do I use the variable as a search condition? It obvious way, you would think would be to do this (carrying on from the above query):&lt;BR /&gt;
&lt;CODE&gt;| search $jobsearch$&lt;/CODE&gt;&lt;BR /&gt;
which doesn't work / returns nothing.&lt;/P&gt;

&lt;P&gt;The other way that seemed promising was using macros.&lt;BR /&gt;
&lt;CODE&gt;[stringsearch(1)]&lt;BR /&gt;
args=sstring&lt;BR /&gt;
definition = search $sstring$&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Which doesn't seem to work either, using these examples:&lt;/P&gt;

&lt;P&gt;This works:&lt;BR /&gt;
&lt;CODE&gt;eventtype=my_eventtype | &lt;CODE&gt;stringsearch(mystringtosearch)&lt;/CODE&gt;&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;This doesn't:&lt;BR /&gt;
&lt;CODE&gt;eventtype=my_eventtype | eval var="mystringtosearch" | &lt;CODE&gt;stringsearch($var$)&lt;/CODE&gt;&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Does anyone know of a way of using strings in variables as conditions?&lt;/P&gt;

&lt;P&gt;Thanks and best regards,&lt;BR /&gt;
Alex&lt;/P&gt;</description>
    <pubDate>Fri, 22 Apr 2016 10:28:52 GMT</pubDate>
    <dc:creator>alekksi</dc:creator>
    <dc:date>2016-04-22T10:28:52Z</dc:date>
    <item>
      <title>Search/Macro using a variable as a condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212561#M62262</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;

&lt;P&gt;I am trying to use a variable as a search condition based on input in a text box. In order to make it simpler for users, I want them to be able to enter as many potential search strings as possible.&lt;/P&gt;

&lt;P&gt;Textbox details on the dashboard:&lt;BR /&gt;
&lt;CODE&gt;token: jobname&lt;BR /&gt;
default: *&lt;BR /&gt;
token prefix: job=&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Example input: "string1 string2 string3"&lt;/P&gt;

&lt;P&gt;I then put this through an eval to get a search condition to run:&lt;BR /&gt;
&lt;CODE&gt;eventtype=my_eventtype | rex field="some regex (?moreregex)" | eval jobname="$jobname$" | eval jobsearch=replace(jobname, " ", " OR job=")&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;This will mean that the variable jobsearch will be:&lt;BR /&gt;
&lt;CODE&gt;"job=string1 OR job=string2 OR job=string3"&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;How then do I use the variable as a search condition? It obvious way, you would think would be to do this (carrying on from the above query):&lt;BR /&gt;
&lt;CODE&gt;| search $jobsearch$&lt;/CODE&gt;&lt;BR /&gt;
which doesn't work / returns nothing.&lt;/P&gt;

&lt;P&gt;The other way that seemed promising was using macros.&lt;BR /&gt;
&lt;CODE&gt;[stringsearch(1)]&lt;BR /&gt;
args=sstring&lt;BR /&gt;
definition = search $sstring$&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Which doesn't seem to work either, using these examples:&lt;/P&gt;

&lt;P&gt;This works:&lt;BR /&gt;
&lt;CODE&gt;eventtype=my_eventtype | &lt;CODE&gt;stringsearch(mystringtosearch)&lt;/CODE&gt;&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;This doesn't:&lt;BR /&gt;
&lt;CODE&gt;eventtype=my_eventtype | eval var="mystringtosearch" | &lt;CODE&gt;stringsearch($var$)&lt;/CODE&gt;&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Does anyone know of a way of using strings in variables as conditions?&lt;/P&gt;

&lt;P&gt;Thanks and best regards,&lt;BR /&gt;
Alex&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2016 10:28:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212561#M62262</guid>
      <dc:creator>alekksi</dc:creator>
      <dc:date>2016-04-22T10:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: Search/Macro using a variable as a condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212562#M62263</link>
      <description>&lt;P&gt;Try these&lt;/P&gt;

&lt;P&gt;Using macro&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eventtype=my_eventtype | eval jobname="$jobname$" | eval jobsearch=replace(jobname, " ", " OR job=") | `stringsearch(jobsearch)`
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Using subsearch (will require to remove "token prefix: job=" from text box)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eventtype=my_eventtype [| gentimes start=-1 |  eval job="$jobname$" | makemv job | mvexpand job | table job ] | rest of the search
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Apr 2016 15:04:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212562#M62263</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-04-22T15:04:32Z</dc:date>
    </item>
    <item>
      <title>Re: Search/Macro using a variable as a condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212563#M62264</link>
      <description>&lt;P&gt;Thanks for your suggestions!&lt;/P&gt;

&lt;P&gt;Unfortunately the top example using a macro doesn't work -- that literally searches the string "jobsearch"&lt;/P&gt;

&lt;P&gt;The bottom one I can't seem to work into my full search.&lt;BR /&gt;
&lt;CODE&gt;eventtype=my_eventtype source="*logfile.log"&lt;BR /&gt;
| rex "[Tt]he job '(?[^']+)'"&lt;BR /&gt;
[ | gentimes start=-1&lt;BR /&gt;
| eval job="email alerts"&lt;BR /&gt;
| makemv job&lt;BR /&gt;
| mvexpand job&lt;BR /&gt;
| table job ]&lt;BR /&gt;
| transaction host job session startswith="Started running the job" endswith="has succeeded"&lt;BR /&gt;
| table _time  session job  duration&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Gives me an error:&lt;BR /&gt;
&lt;CODE&gt;Error in 'rex' command: Invalid argument: '('&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;PS: How do I use that codeblock you've used? I'm not sure what the markup is for it and the code html tag is awful.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2016 15:52:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212563#M62264</guid>
      <dc:creator>alekksi</dc:creator>
      <dc:date>2016-04-22T15:52:22Z</dc:date>
    </item>
    <item>
      <title>Re: Search/Macro using a variable as a condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212564#M62265</link>
      <description>&lt;P&gt;The bottom search for your full search would be like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eventtype=my_eventtype source="*logfile.log" | rex "[Tt]he job '(?&amp;lt;job&amp;gt;[^']+)'" | search [ | gentimes start=-1 | eval job="email alerts" | makemv job | mvexpand job | table job ] | transaction host job session startswith="Started running the job" endswith="has succeeded" | table _time session job duration
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;To use code block, either use the '101010' type button above the text area OR add 4 spaces at the start of the line.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2016 16:25:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212564#M62265</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-04-22T16:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: Search/Macro using a variable as a condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212565#M62266</link>
      <description>&lt;P&gt;Mate, you're a lifesaver on both counts. Thank you very much!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2016 08:04:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-Macro-using-a-variable-as-a-condition/m-p/212565#M62266</guid>
      <dc:creator>alekksi</dc:creator>
      <dc:date>2016-04-25T08:04:52Z</dc:date>
    </item>
  </channel>
</rss>

