<?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 a dynamic string substitution to insert specific parameters into specific place in string in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585333#M203919</link>
    <description>&lt;P&gt;Dear Splunk community&lt;/P&gt;
&lt;P&gt;I need help with a presumably easy task, but it had already cost me quite a while.&lt;/P&gt;
&lt;P&gt;I'm trying to make a dynamic string substitution to insert specific parameters into specific place in string.&lt;/P&gt;
&lt;P&gt;in example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval message="blablabla [%2] blablabla [%1] blablabla [%3]" 
| eval param="param1:param2:param3"&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where %1 is the respective position in param string (colon separated)&lt;/P&gt;
&lt;P&gt;The resulting string would be (note that it is not in param index order):&lt;/P&gt;
&lt;P&gt;"blablabla [param2] blablabla [param1] blablabla [param3]"&lt;/P&gt;
&lt;P&gt;The number of parameters and indexes in message varies (usually from 1 to 4, but can also be none).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried to split it into mv fields and make some multi value indexed substitution,&amp;nbsp; and then use a foreach statement or mvjoin but frankly i failed.&lt;/P&gt;
&lt;P&gt;I've also considered some hard regex work but i'm not even sure if its possible to work&lt;/P&gt;
&lt;P&gt;Please note that I am limited to Splunk Enterprise 6.5.10&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MM&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Feb 2022 15:44:50 GMT</pubDate>
    <dc:creator>mmacalik</dc:creator>
    <dc:date>2022-02-16T15:44:50Z</dc:date>
    <item>
      <title>How to make a dynamic string substitution to insert specific parameters into specific place in string</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585333#M203919</link>
      <description>&lt;P&gt;Dear Splunk community&lt;/P&gt;
&lt;P&gt;I need help with a presumably easy task, but it had already cost me quite a while.&lt;/P&gt;
&lt;P&gt;I'm trying to make a dynamic string substitution to insert specific parameters into specific place in string.&lt;/P&gt;
&lt;P&gt;in example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval message="blablabla [%2] blablabla [%1] blablabla [%3]" 
| eval param="param1:param2:param3"&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where %1 is the respective position in param string (colon separated)&lt;/P&gt;
&lt;P&gt;The resulting string would be (note that it is not in param index order):&lt;/P&gt;
&lt;P&gt;"blablabla [param2] blablabla [param1] blablabla [param3]"&lt;/P&gt;
&lt;P&gt;The number of parameters and indexes in message varies (usually from 1 to 4, but can also be none).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried to split it into mv fields and make some multi value indexed substitution,&amp;nbsp; and then use a foreach statement or mvjoin but frankly i failed.&lt;/P&gt;
&lt;P&gt;I've also considered some hard regex work but i'm not even sure if its possible to work&lt;/P&gt;
&lt;P&gt;Please note that I am limited to Splunk Enterprise 6.5.10&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MM&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 15:44:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585333#M203919</guid>
      <dc:creator>mmacalik</dc:creator>
      <dc:date>2022-02-16T15:44:50Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585334#M203920</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i am not entirely sure i understood your issue...&lt;/P&gt;&lt;P&gt;is that what you need?&lt;/P&gt;&lt;P&gt;| makeresults&lt;BR /&gt;| eval var1 = "11"&lt;BR /&gt;| eval var2 = "12"&lt;BR /&gt;| eval var3 = "13"&lt;BR /&gt;| eval message="blablabla [" . $var1$ . "] blablabla [" . $var2$ . "] blablabla [" . $var3$ . "]"&lt;BR /&gt;| table message&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 12:45:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585334#M203920</guid>
      <dc:creator>Software-Simian</dc:creator>
      <dc:date>2022-02-16T12:45:36Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585335#M203921</link>
      <description>This seems to be a solution to some issue, but can you describe what was your original challenge which you are trying to solve? Maybe there are some other way to do it?</description>
      <pubDate>Wed, 16 Feb 2022 12:45:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585335#M203921</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2022-02-16T12:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585339#M203924</link>
      <description>&lt;P&gt;Additional details:&lt;/P&gt;&lt;P&gt;The data I'm analysing consists (among other) production process parameter's and human readable message template to populate with those parameters.&lt;/P&gt;&lt;P&gt;Basically its thousands of message - param pairs which needs to be combined into human readable format.&lt;BR /&gt;&lt;BR /&gt;More insight example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval message="Product [%2] is on hold in process [%1] hold description: [%3]" 
| eval param="PID1000:ABCD123:visual defect"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which would result in human readable format:&lt;/P&gt;&lt;P&gt;Product [ABCD123] is on hold in process [PID1000] hold description: [visual defect]&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;another example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval message="Material [%1] already assigned to [%2]" 
| eval param="MAT1234:ABCD123"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should result in :&lt;BR /&gt;Material [MAT1234] already assigned to [ABCD123]&lt;/P&gt;&lt;P&gt;Although making&amp;nbsp; spearate&amp;nbsp; eval would work for specific example, the number of parameters can differ as well their order in message (thus numerated indexes ).&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;For example if i would do it in any typical language i would split the param string into a list (or array), and then inserted indexed values into proper positions in message string.&lt;/P&gt;&lt;P&gt;Is something like that possible?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 13:01:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585339#M203924</guid>
      <dc:creator>mmacalik</dc:creator>
      <dc:date>2022-02-16T13:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585341#M203925</link>
      <description>&lt;P class="lia-align-left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;&lt;FONT color="#008000"&gt;&lt;EM&gt;For example if i would do it in any typical language i would split the param string into a list (or array), and then inserted indexed values into proper positions in message string.&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-align-left"&gt;&lt;SPAN&gt;So you have a basic set of messages with placeholders...okay where is the problem?&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="lia-align-left"&gt;You define a lookup with those message and assign an ID...depending on the ID a specific message template is loaded. Now the only thing missing are the values...well those are being retrieved with the same ID and then inserted into the template as demonstrated.&lt;/P&gt;&lt;P class="lia-align-left"&gt;Depending on the amount this can be done with CASE or a separate lookup table&lt;/P&gt;&lt;P&gt;| makeresults&lt;/P&gt;&lt;P&gt;| eval ID = 2&lt;BR /&gt;| eval var1 = Case(ID=="1", "11", 1==1, "21")&lt;BR /&gt;| eval var2 = Case(ID=="1", "12", 1==1, "22")&lt;BR /&gt;| eval var3 = Case(ID=="1", "13", 1==1, "23")&lt;BR /&gt;| eval message = Case(ID=="1", "Product [" . $var1$ . "] is on hold in process [" . $var2$ . "] hold description: [" . $var3$ . "]",&lt;BR /&gt;1==1, "Material [" . $var1$ . "] already assigned to [" . $var2$ . "]")&lt;/P&gt;&lt;P&gt;| table message&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Please change ID value and append Case options OR create Lookups&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 13:33:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585341#M203925</guid>
      <dc:creator>Software-Simian</dc:creator>
      <dc:date>2022-02-16T13:33:08Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585347#M203929</link>
      <description>&lt;LI-CODE lang="markup"&gt;| makeresults
| eval message="blablabla [%2] blablabla [%1] blablabla [%3]"
| eval param="paramA:paramB:paramC"
| rex field=message max_match=0 "\[\%(?&amp;lt;arg&amp;gt;\d+)\]"
| stats values(message) as message values(param) as param by arg
| eval arg="arg".arg
| xyseries message arg param
| foreach arg*
    [| eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=mvindex(split(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,":"),&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;-1)
    | eval message=replace(message,"\[\%&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;\]",&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)]&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 16 Feb 2022 13:39:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585347#M203929</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2022-02-16T13:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585348#M203930</link>
      <description>&lt;P&gt;If/when&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/240008"&gt;@Software-Simian&lt;/a&gt;&amp;nbsp;'s solutions didn't help you, you could try to splunk's printf conversion function. But then you have issues about orders of parameters and also how to create arguments as a list of values.&lt;/P&gt;&lt;P&gt;So probably the easiest and most robust solution is to create your own splunk external command which take those message and params as an arguments and then return a string which contains what you are needing. I suppose that there are already some examples on splunkbase, which you can use as a starting point.&lt;/P&gt;&lt;P&gt;r. Ismo&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;seems to solve this already &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; Thanks&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;I learn something new!&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 13:47:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585348#M203930</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2022-02-16T13:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585350#M203931</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp; &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;&amp;nbsp; that's exactly what i was looking for.&lt;/P&gt;&lt;P&gt;Now to analyze what is actually going on... &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/240008"&gt;@Software-Simian&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The lookup aproach seems also good, but not feasable when there are more than 4000 message templates in 4 languages (different syntax thus different param order).&lt;/P&gt;</description>
      <pubDate>Wed, 16 Feb 2022 13:55:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585350#M203931</guid>
      <dc:creator>mmacalik</dc:creator>
      <dc:date>2022-02-16T13:55:39Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585355#M203932</link>
      <description>&lt;LI-CODE lang="markup"&gt;``` Comments don't work this way until later versions of splunk, but using this format for convenience ```
| makeresults
| eval message="blablabla [%2] blablabla [%1] blablabla [%3]"
| eval param="paramA:paramB:paramC"
``` Extract all the argument placeholder numbers ```
| rex field=message max_match=0 "\[\%(?&amp;lt;arg&amp;gt;\d+)\]"
``` Separate them into different events - you might need to add some extra commands here to maintain which arguments came from which event. Let me know if you are unsure how to do this ```
| stats values(message) as message values(param) as param by arg
``` Prefix the placeholder numbers with a known string ```
| eval arg="arg".arg
``` Convert the placeholder rows to columns ```
| xyseries message arg param
``` For each placeholder column (field) - this is why the known prefix is important ```
| foreach arg*
    ``` Use MATCHSEG1 to get the placeholder number from the field name ```
    ``` Use the placeholder number to get the corresponding parameter value ```
    [| eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=mvindex(split(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,":"),&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;-1)
    ``` Replace the placeholder in the message with the parameter value ```
    | eval message=replace(message,"\[\%&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;\]",&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)]
``` Robert is your father's brother! ```&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 16 Feb 2022 14:07:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585355#M203932</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2022-02-16T14:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585497#M203991</link>
      <description>&lt;P&gt;I modified it a little so it can work with multiple rows.&lt;/P&gt;&lt;P&gt;In production environment i have much more fields that can distinguish a row but for the sake of example i added counter and param as |stats by argument.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;|makeresults count=4
|streamstats count
|eval message=case(count=1, "blablabla [%2] blablabla [%1] blablabla [%3]", count=2, "blablabla [%2] blablabla [%1] blablabla", count=3, "blablabla [%2] blablabla [%1] blablabla [%3]", count=4, "blablabla [%1]")
|eval param=case(count=1, "paramA:paramB:paramC", count=2, "paramD:paramE", count=3, "paramF:paramG:paramH", count=4, "paramI")
|rename count as counter
|fields message, param, counter

| rex field=message max_match=0 "\[\%(?&amp;lt;arg&amp;gt;\d+)\]"
| stats values(message) as message values(param) as params by counter, param arg 
| eval arg="arg".arg
| eval message=counter.":".message
| xyseries message arg param
| foreach arg*
    [| eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=mvindex(split(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,":"),&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;-1)
    | eval message=if(isnull(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;), message, replace(message,"\[\%&amp;lt;&amp;lt;MATCHSEG1&amp;gt;&amp;gt;\]",&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;))]
| eval message=mvindex(split(message,":"),1)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Feb 2022 09:24:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585497#M203991</guid>
      <dc:creator>mmacalik</dc:creator>
      <dc:date>2022-02-17T09:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic string substitution</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585502#M203992</link>
      <description>&lt;P&gt;Yes, streamstats count is the way I would have done it too&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Feb 2022 09:48:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-make-a-dynamic-string-substitution-to-insert-specific/m-p/585502#M203992</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2022-02-17T09:48:21Z</dc:date>
    </item>
  </channel>
</rss>

