<?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 replace query input variables and replace with variable values. in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/552816#M9890</link>
    <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp; : Thank you for the pointer. The query which I have given was an example of 3 parameters, but there are scenarios where we do have parameters ranging from 2-30 in that manual hard code of parameters would not work &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 24 May 2021 17:25:47 GMT</pubDate>
    <dc:creator>harry3988</dc:creator>
    <dc:date>2021-05-24T17:25:47Z</dc:date>
    <item>
      <title>How to replace query input variables and replace with variable values.</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/552795#M9888</link>
      <description>&lt;P&gt;Hello ,&lt;/P&gt;&lt;P&gt;I have an event log being printed as :&lt;/P&gt;&lt;P&gt;Mon May 24 07:15:19 PDT 2021&amp;nbsp;buildAndExecuteQuery query SELECT emp.Name,emp.age,emp.address from emp where (emp.Region LIKE :1 OR emp:Region LIKE :2) AND emp.department LIKE :3&amp;nbsp; with parameters&amp;nbsp;[%region07%, %region09%,%0840%].&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I m trying to extract an sql query field with the following desired output&amp;nbsp;&lt;/P&gt;&lt;P&gt;SELECT emp.Name,emp.age,emp.address from emp where (emp.Region LIKE : "region07" OR emp:Region LIKE :"region09") AND emp.department LIKE :"0840 "&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tried extracting the input variable fields and the parameter fields using rex command command and not able to move further on how to replace the value in the query .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;Harris&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 14:51:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/552795#M9888</guid>
      <dc:creator>harry3988</dc:creator>
      <dc:date>2021-05-24T14:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace query input variables and replace with variable values.</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/552802#M9889</link>
      <description>&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw="Mon May 24 07:15:19 PDT 2021 buildAndExecuteQuery query SELECT emp.Name,emp.age,emp.address from emp where (emp.Region LIKE :1 OR emp:Region LIKE :2) AND emp.department LIKE :3  with parameters [%region07%, %region09%,%0840%]."
| rex "query (?&amp;lt;query&amp;gt;.*)\s+with\sparameters\s\[%(?&amp;lt;one&amp;gt;[^%]+)%,\s*%(?&amp;lt;two&amp;gt;[^%]+)%,\s*%(?&amp;lt;three&amp;gt;[^%]+)%\]"
| eval query=replace(query,":1",":\"".one."\"")
| eval query=replace(query,":2",":\"".two."\"")
| eval query=replace(query,":3",":\"".three."\"")&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 24 May 2021 15:17:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/552802#M9889</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2021-05-24T15:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace query input variables and replace with variable values.</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/552816#M9890</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp; : Thank you for the pointer. The query which I have given was an example of 3 parameters, but there are scenarios where we do have parameters ranging from 2-30 in that manual hard code of parameters would not work &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 17:25:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/552816#M9890</guid>
      <dc:creator>harry3988</dc:creator>
      <dc:date>2021-05-24T17:25:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace query input variables and replace with variable values.</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/552940#M9891</link>
      <description>&lt;P&gt;This can be done with the use of macros&lt;/P&gt;&lt;P&gt;Firstly, the changes to the search&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw="Mon May 24 07:15:19 PDT 2021 buildAndExecuteQuery query SELECT emp.Name,emp.age,emp.address from emp where (emp.Region LIKE :1 OR emp:Region LIKE :2)  with parameters [%region07%, %region09%].
Mon May 24 07:15:19 PDT 2021 buildAndExecuteQuery query SELECT emp.Name,emp.age,emp.address from emp where (emp.Region LIKE :1 OR emp:Region LIKE :2) AND emp.department LIKE :3  with parameters [%region07%, %region09%,%0840%]."
| multikv noheader=t 
| fields _raw
| fields - _time


| rex "query (?&amp;lt;query&amp;gt;.*)\s+with\sparameters\s\[(?&amp;lt;parameters&amp;gt;[^\]]+)\]"
| rex field=parameters max_match=0 "%(?&amp;lt;parameter&amp;gt;[^%]+)%"
| eval count=mvcount(parameter)
| eval expanded=query
| rex field=expanded mode=sed "s/(?&amp;lt;param&amp;gt; :\d+)/\1 /g"
| streamstats count as row
| appendpipe
    [| `generate_fields("param",count)`
    | streamstats count as row ]
| stats list(*) as * list(_*) as _* by row
| foreach param_*
    [| eval expanded=if(count &amp;gt;= &amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;,replace(expanded," :".&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;." "," :".mvindex(parameter,&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;-1)), expanded)]
| fields - count param* row&lt;/LI-CODE&gt;&lt;P&gt;The first part sets up some example data - I duplicated the rows more or less to show it working with different numbers of parameters.&lt;/P&gt;&lt;P&gt;The next part splits off the query string and parameters as splits the parameters into a mv field&lt;/P&gt;&lt;P&gt;Next count the number of parameters for each event and take a copy of the query string (the copy isn't strictly necessary but does make it easier to see the before and after).&lt;/P&gt;&lt;P&gt;Then add a space after each of the :parameters to help distinguish 1 from 10 for example.&lt;/P&gt;&lt;P&gt;Tag each event with a row number so that the generated fields can be joined back up. There is possibly another way to do this, but I haven't worked that one through.&lt;/P&gt;&lt;P&gt;Now, appendpipe calling the macro (to be defined later) and tagging these additional events in the same manner.&lt;/P&gt;&lt;P&gt;Join the events up again using the row as a common field.&lt;/P&gt;&lt;P&gt;Now for each of the added fields, replace the corresponding placeholder in the query string with the corresponding parameter value.&lt;/P&gt;&lt;P&gt;Now for the macro, or rather macros. The first macro - generate_fields - is defined as such&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;eval i=mvjoin(mvrange(1,$count_INPUT$+1)," ")
| map maxsearches=1000 search="| `generate_fields_inner($prefix_INPUT$,$$i$$)`"&lt;/LI-CODE&gt;&lt;P&gt;This macro takes two named arguments -&amp;nbsp;prefix_INPUT,count_INPUT&lt;/P&gt;&lt;P&gt;Note that it uses map with maxsearches=1000, this is to avoid potentially crippling splunk. Also, this macro calls another macro - generate_fields_inner - which does the bulk of the work. This first macro is designed to expand the count to a string of space separated values&lt;/P&gt;&lt;P&gt;The second macro - generate_fields_inner - is defined as such&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;makeresults | fields - _time
| foreach $integer_list_value_INPUT$
    [| eval _counter = if(isnull(_counter),1,_counter + 1)
    | eval $prefix_INPUT$_{_counter}=_counter]
| fields - _counter&lt;/LI-CODE&gt;&lt;P&gt;This macro also takes two arguments -&amp;nbsp;prefix_INPUT,integer_list_value_INPUT&lt;/P&gt;&lt;P&gt;This macro is designed to generate fields based on the integers in the list containing the corresponding integer. This allows the main search to use the value (-1) to index into the parameter list to extract the corresponding argument value.&lt;/P&gt;&lt;P&gt;Thanks to&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/1406"&gt;@woodcock&lt;/a&gt;&amp;nbsp;for the inspiration for this solution.&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 13:23:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/552940#M9891</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2021-05-25T13:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace query input variables and replace with variable values.</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/553028#M9892</link>
      <description>&lt;P&gt;Minor improvements so that map is only run once&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw="Mon May 24 07:15:19 PDT 2021 buildAndExecuteQuery query SELECT emp.Name,emp.age,emp.address from emp where (emp.Region LIKE :1 OR emp:Region LIKE :2)  with parameters [%region07%, %region09%].
Mon May 24 07:15:19 PDT 2021 buildAndExecuteQuery query SELECT emp.Name,emp.age,emp.address from emp where (emp.Region LIKE :1 OR emp:Region LIKE :2) AND emp.department LIKE :3  with parameters [%region07%, %region09%,%0840%]."
| multikv noheader=t 
| fields _raw
| fields - _time


| rex "query (?&amp;lt;query&amp;gt;.*)\s+with\sparameters\s\[(?&amp;lt;parameters&amp;gt;[^\]]+)\]"
| rex field=parameters max_match=0 "%(?&amp;lt;parameter&amp;gt;[^%]+)%"
| eval count=mvcount(parameter)
| eventstats max(count) as countmax
| eval expanded=query
| rex field=expanded mode=sed "s/ :(?&amp;lt;d&amp;gt;\d+)/ :param_\1 /g"
| appendpipe
    [| `generate_fields("param",countmax)` ]
| reverse
| filldown param_*
| where isnotnull(_raw)
| foreach param_*
    [| eval expanded=if(count &amp;gt;= &amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;,replace(expanded," :&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; "," :".mvindex(parameter,&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;-1)), expanded)]
| fields - count countmax param*
| reverse&lt;/LI-CODE&gt;&lt;P&gt;generate_fields&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;eval i=mvjoin(mvrange(1,$count_INPUT$+1)," ") | map maxsearches=1 search="| `generate_fields_inner($prefix_INPUT$,$$i$$)`"&lt;/LI-CODE&gt;&lt;P&gt;No changes to generate_fields_inner&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 21:02:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/553028#M9892</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2021-05-25T21:02:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace query input variables and replace with variable values.</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/553460#M9893</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp; : Thank you for the solution , it has worked with different iterations.&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 08:11:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/553460#M9893</guid>
      <dc:creator>harry3988</dc:creator>
      <dc:date>2021-05-28T08:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace query input variables and replace with variable values.</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/553617#M9894</link>
      <description>&lt;P&gt;I found a simpler solution - the ":" are in the mvzip and trim because they appear in your example but I am not sure if they are necessary - it is assumed with this solution that the parameters in the original query string are in ascending order - if not, you could do something about rearranging the parameter mv field so that this is in the corresponding order are the placeholders in the query string&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw="Mon May 24 07:15:19 PDT 2021 buildAndExecuteQuery query SELECT emp.Name,emp.age,emp.address from emp where (emp.Region LIKE :1 OR emp:Region LIKE :2)  with parameters [%region07%, %region09%].
Mon May 24 07:15:19 PDT 2021 buildAndExecuteQuery query SELECT emp.Name,emp.age,emp.address from emp where (emp.Region LIKE :1 OR emp:Region LIKE :2) AND emp.department LIKE :3  with parameters [%region07%, %region09%,%0840%]."
| multikv noheader=t 
| fields _raw
| fields - _time


| rex "query (?&amp;lt;query&amp;gt;.*)\s+with\sparameters\s\[(?&amp;lt;parameters&amp;gt;[^\]]+)\]"
| rex field=parameters max_match=0 "%(?&amp;lt;parameter&amp;gt;[^%]+)%"
| eval parameter=mvappend(parameter,"")
| eval expanded=split(replace(query,":\d+","#"),"#")
| eval expanded=mvzip(expanded,parameter,":")
| eval expanded=mvjoin(expanded,"")
| eval expanded=trim(expanded,":")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 May 2021 08:12:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-replace-query-input-variables-and-replace-with-variable/m-p/553617#M9894</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2021-05-29T08:12:30Z</dc:date>
    </item>
  </channel>
</rss>

