<?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 Extracting key and value from substring in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668623#M112062</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I'm trying to corral a string into new field and value and having trouble. I've used eval / split / mvexpand....&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The string looks like this. Its actually a field in an event:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;field_id=/key1/value1/key2/value2/key3/value3/key4/value4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The end goal is to have new fields. Like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;field_key1=value1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;filed_key2=value2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;So i can now search, for example, if field_key1='the value of something"&lt;/SPAN&gt;&lt;/P&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>Tue, 14 Nov 2023 17:46:40 GMT</pubDate>
    <dc:creator>brdr</dc:creator>
    <dc:date>2023-11-14T17:46:40Z</dc:date>
    <item>
      <title>Extracting key and value from substring</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668623#M112062</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I'm trying to corral a string into new field and value and having trouble. I've used eval / split / mvexpand....&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The string looks like this. Its actually a field in an event:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;field_id=/key1/value1/key2/value2/key3/value3/key4/value4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The end goal is to have new fields. Like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;field_key1=value1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;filed_key2=value2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;So i can now search, for example, if field_key1='the value of something"&lt;/SPAN&gt;&lt;/P&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>Tue, 14 Nov 2023 17:46:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668623#M112062</guid>
      <dc:creator>brdr</dc:creator>
      <dc:date>2023-11-14T17:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting key and value from substring</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668632#M112064</link>
      <description>&lt;LI-CODE lang="markup"&gt;| rex field=field_id max_match=0 "/(?&amp;lt;key&amp;gt;[^/]+)/(?&amp;lt;value&amp;gt;[^/]+)"
| eval row=mvrange(0,mvcount(key))
| streamstats count as _row
| mvexpand row
| eval name="field_".mvindex(key,row)
| eval {name}=mvindex(value,row)
| fields - key value name row
| stats values(*) as * by _row&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 14 Nov 2023 18:19:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668632#M112064</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2023-11-14T18:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting key and value from substring</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668675#M112075</link>
      <description>&lt;P&gt;If you have a known max limit of keys, then you can do it without the mvexpand, which if you have a large dataset, can hit memory issues.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval field_id="/key1/value1/key2/value2/key3/value3/key4/value4"
| rex field=field_id max_match=0 "/(?&amp;lt;k&amp;gt;[^/]*)/(?&amp;lt;v&amp;gt;[^/]*)"
| foreach 0 1 2 3 4 5 6 7 8 9 10[ eval _k=mvindex(k, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;), {_k}=mvindex(v, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;) ]&lt;/LI-CODE&gt;&lt;P&gt;Just put in the foreach statement the maximum number of possible key/value pairs you have.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2023 03:07:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668675#M112075</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2023-11-15T03:07:42Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting key and value from substring</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668797#M112100</link>
      <description>&lt;P&gt;Thank you both ITWhisperer and bowesmana!!!&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Will try these out&lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2023 23:35:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668797#M112100</guid>
      <dc:creator>brdr</dc:creator>
      <dc:date>2023-11-15T23:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting key and value from substring</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668802#M112101</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/161192"&gt;@brdr&lt;/a&gt;&amp;nbsp;...the above 2 SPL are working fine as you can see on the screenshots below.&lt;BR /&gt;the easiest one is the split command:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval field_id="/key1/value1/key2/value2/key3/value3/key4/value4"
| eval temp=split(field_id,"/") | eval field_key1=mvindex(temp,2) | eval field_key2=mvindex(temp,4) 
| table field_id field_key1 field_key2&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="streamstats.jpg" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/28139iECE20FD496DA791A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="streamstats.jpg" alt="streamstats.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="split.jpg" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/28140iFB5E842BDF919FA5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="split.jpg" alt="split.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="foreach-rex.jpg" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/28141iF3CD15D77CD0484F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="foreach-rex.jpg" alt="foreach-rex.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;   &lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2023 23:56:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668802#M112101</guid>
      <dc:creator>inventsekar</dc:creator>
      <dc:date>2023-11-15T23:56:05Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting key and value from substring</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668916#M112126</link>
      <description>&lt;LI-CODE lang="markup"&gt;| makeresults
| eval field_id="/key1/value1/key2/value2/key3/value3/key4/value4"
| rex field=field_id max_match=0 "/(?&amp;lt;key&amp;gt;[^/]*)/(?&amp;lt;value&amp;gt;[^/]*)"
| foreach 0 1 2 3 4 5 6 7 8 9 10[ eval _key=mvindex(key, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;), {_key}=mvindex(value, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;) ]&lt;/LI-CODE&gt;&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt;&amp;nbsp;.. instead of k and v, i used key and value, it works fine as well.&amp;nbsp;&lt;/P&gt;&lt;P&gt;could you pls explain how the last eval works (why do you use "eval _k")&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2023 21:24:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668916#M112126</guid>
      <dc:creator>inventsekar</dc:creator>
      <dc:date>2023-11-16T21:24:01Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting key and value from substring</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668934#M112130</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/80737"&gt;@inventsekar&lt;/a&gt;&amp;nbsp;You can use whatever two variables you like, a/b, k/v, key/value&lt;/P&gt;&lt;P&gt;In the foreach using a var name with _ prefix means that it will not be generally visible as a field, so in case you forget to remove the field _key, it will not be seen as part of the data. I often use that just to make sure temporary fields are hidden and don't become part of the working dataset.&lt;/P&gt;&lt;P&gt;The syntax &lt;STRONG&gt;{_key}=mvindex(value,&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)&lt;/STRONG&gt; uses Splunk's encoding to create a new field (left hand side) that has the name of the VALUE of _key and it takes the n'th multivalue element from the value MV based on &lt;STRONG&gt;&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;&lt;/STRONG&gt; which is effectively a loop of the values of the foreach statement &lt;STRONG&gt;0 1 2 3 4&lt;/STRONG&gt;...&lt;/P&gt;&lt;P&gt;It's the same as doing this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| fields - _time
| eval key="NAME", value="ANTONY"
| eval {key}=value&lt;/LI-CODE&gt;&lt;P&gt;where you will end up with a new field called NAME with the value of ANTONY&lt;/P&gt;&lt;P&gt;There should really be cleanup to remove the temporary field names k,v,_k, so a | fields statement would be a good idea at the end.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2023 23:47:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668934#M112130</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2023-11-16T23:47:15Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting key and value from substring</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668937#M112131</link>
      <description>&lt;P&gt;Great, thanks a lot&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt;&amp;nbsp;,..much appreciated !&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2023 00:13:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Extracting-key-and-value-from-substring/m-p/668937#M112131</guid>
      <dc:creator>inventsekar</dc:creator>
      <dc:date>2023-11-17T00:13:06Z</dc:date>
    </item>
  </channel>
</rss>

