<?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: rex and multi-value fields, assigning key value pairs in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613177#M213101</link>
    <description>&lt;P&gt;The {xx}=yy syntax will not work well with MV fields, so without using zip/mvexpand, the way to do this is with foreach&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval xml="&amp;lt;top&amp;gt;&amp;lt;N&amp;gt;N1&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;V1&amp;lt;/V&amp;gt;&amp;lt;N&amp;gt;N2&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;V2&amp;lt;/V&amp;gt;&amp;lt;/top&amp;gt;" 
| rex field=xml max_match=0 "&amp;lt;N&amp;gt;(?&amp;lt;field_name&amp;gt;(.*?))&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;(?&amp;lt;field_value&amp;gt;(.*?))&amp;lt;"
``` Data setup above ```

``` foreach with a fixed set of numbers will cause a 'loop' ```
| foreach 0 1 2 3 4 5 [ eval n=mvindex(field_name, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;), v=mvindex(field_value, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;), {n}=v ]
| fields - n v field_name field_value&lt;/LI-CODE&gt;&lt;P&gt;The 0 1 2 3 4... will define the maximum number of possible values, so if you have an unbounded number, this is not a good solution, but if you know your maximum, then this will work&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 15 Sep 2022 06:39:29 GMT</pubDate>
    <dc:creator>bowesmana</dc:creator>
    <dc:date>2022-09-15T06:39:29Z</dc:date>
    <item>
      <title>Help with rex and multi-value fields, assigning key value pairs?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613170#M213099</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;I have a search which parses key/value pairs out of a strangely-formatted XML field.&amp;nbsp;&amp;nbsp;&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;
&lt;LI-CODE lang="markup"&gt;rex field=xml "&amp;lt;N&amp;gt;(?&amp;lt;field_name&amp;gt;(.*?))&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;(?&amp;lt;field_value&amp;gt;(.*?))&amp;lt;" | eval {field_name}=field_value &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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Above, when there is a single match, this works as expected.&amp;nbsp; I have the field name and the field value available as a field in my results.&amp;nbsp; &amp;nbsp;What I don't know how to do, is make this work for multiple matches.&amp;nbsp; &amp;nbsp;When I run:&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;
&lt;LI-CODE lang="markup"&gt;rex field=xml max_match=0 "&amp;lt;N&amp;gt;(?&amp;lt;field_name&amp;gt;(.*?))&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;(?&amp;lt;field_value&amp;gt;(.*?))&amp;lt;" | eval {field_name}=field_value &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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then both field_name and field_value are multi-value fields.&amp;nbsp; I would like to make each key=value available in the results as I did above.&amp;nbsp; &amp;nbsp; Can anyone give me a pointer on how to accomplish this?&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 14:52:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613170#M213099</guid>
      <dc:creator>Phil_S</dc:creator>
      <dc:date>2022-09-15T14:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: rex and multi-value fields, assigning key value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613177#M213101</link>
      <description>&lt;P&gt;The {xx}=yy syntax will not work well with MV fields, so without using zip/mvexpand, the way to do this is with foreach&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval xml="&amp;lt;top&amp;gt;&amp;lt;N&amp;gt;N1&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;V1&amp;lt;/V&amp;gt;&amp;lt;N&amp;gt;N2&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;V2&amp;lt;/V&amp;gt;&amp;lt;/top&amp;gt;" 
| rex field=xml max_match=0 "&amp;lt;N&amp;gt;(?&amp;lt;field_name&amp;gt;(.*?))&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;(?&amp;lt;field_value&amp;gt;(.*?))&amp;lt;"
``` Data setup above ```

``` foreach with a fixed set of numbers will cause a 'loop' ```
| foreach 0 1 2 3 4 5 [ eval n=mvindex(field_name, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;), v=mvindex(field_value, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;), {n}=v ]
| fields - n v field_name field_value&lt;/LI-CODE&gt;&lt;P&gt;The 0 1 2 3 4... will define the maximum number of possible values, so if you have an unbounded number, this is not a good solution, but if you know your maximum, then this will work&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 06:39:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613177#M213101</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2022-09-15T06:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: rex and multi-value fields, assigning key value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613179#M213102</link>
      <description>&lt;P&gt;Thanks!&amp;nbsp; This is a huge step forward.&amp;nbsp; &amp;nbsp;Unfortunately, the number of items is variable.&amp;nbsp; &amp;nbsp;Is there a way I can rebuild the loop using mvcount() or somethink like that?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;foreach 0...mvcount(field_name) &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Something like the above?&amp;nbsp; &amp;nbsp;Appreciate your help so far!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 06:56:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613179#M213102</guid>
      <dc:creator>Phil_S</dc:creator>
      <dc:date>2022-09-15T06:56:26Z</dc:date>
    </item>
    <item>
      <title>Re: rex and multi-value fields, assigning key value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613186#M213103</link>
      <description>&lt;P&gt;I've tried this some times in the past to see if it's possible to create macros to do this, for example you can build the loop string easily enough, e.g.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval n=mvcount(field_name)
| eval f=mvjoin(mvrange(0, n, 1), " ")&lt;/LI-CODE&gt;&lt;P&gt;which creates the string 0 1 2 etc&lt;/P&gt;&lt;P&gt;but it seems to be impossible to get this string to be used in foreach, either directly or in the macro, as macros are expanded before the search.&lt;/P&gt;&lt;P&gt;However, Splunk 9 has some enhancements to foreach that specifically handle MV fields, but I've not got a v9 instance.&lt;/P&gt;&lt;P&gt;It doesn't matter if the number of values is variable, in that as long as you have enough 1 2 3 4 etc to handle the max you will ever see, that's fine.&lt;/P&gt;&lt;P&gt;Here's another approach to it using mvzip/mvexpand/stats, which will be potentially expensive and requires a common ID to stats back together again&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults count=2
| streamstats c as ID
| eval xml="&amp;lt;top&amp;gt;&amp;lt;N&amp;gt;N1&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;V".(random() % 100)."&amp;lt;/V&amp;gt;&amp;lt;N&amp;gt;N2&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;V".(random() % 100)."&amp;lt;/V&amp;gt;&amp;lt;/top&amp;gt;" 
| rex field=xml max_match=0 "&amp;lt;N&amp;gt;(?&amp;lt;field_name&amp;gt;(.*?))&amp;lt;/N&amp;gt;&amp;lt;V&amp;gt;(?&amp;lt;field_value&amp;gt;(.*?))&amp;lt;"
| eval c=mvzip(field_name, field_value, "####")
| mvexpand c
| rex field=c "(?&amp;lt;field_name&amp;gt;(.*))####(?&amp;lt;field_value&amp;gt;(.*))"
| eval {field_name}=field_value
| stats values(N*) as N* by ID&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;is normally good at these quirky questions and will probably have a simple one liner to do it&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 07:46:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613186#M213103</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2022-09-15T07:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: rex and multi-value fields, assigning key value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613201#M213104</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;P&gt;It doesn't matter if the number of values is variable, in that as long as you have enough 1 2 3 4 etc to handle the max you will ever see, that's fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Perfect, that works for me.&amp;nbsp; Thanks!!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 09:12:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Help-with-rex-and-multi-value-fields-assigning-key-value-pairs/m-p/613201#M213104</guid>
      <dc:creator>Phil_S</dc:creator>
      <dc:date>2022-09-15T09:12:12Z</dc:date>
    </item>
  </channel>
</rss>

