<?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 do I merge a variable number of fields and expand rows? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444389#M126027</link>
    <description>&lt;P&gt;It is strange that it is not working for you with single DB, can you please replace &lt;CODE&gt;&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;&lt;/CODE&gt; with &lt;CODE&gt;'&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'&lt;/CODE&gt; in all 3 &lt;CODE&gt;foreach&lt;/CODE&gt; command ? &lt;CODE&gt;foreach&lt;/CODE&gt; command repeat &lt;CODE&gt;eval&lt;/CODE&gt; for every DB*_DBNAME so if you have only DB1_DBNAME then it will do &lt;CODE&gt;eval&lt;/CODE&gt; only once and zip &lt;CODE&gt;DBNAME (which is 0)&lt;/CODE&gt; and &lt;CODE&gt;DB1_DBNAME&lt;/CODE&gt;.  &lt;/P&gt;

&lt;P&gt;Search query which I have provided is working with 1 Database as well, I have tested below query in Splunk 7.1.2 and it is generating output.&lt;/P&gt;

&lt;P&gt;Below run anywhere search I am running and it is generating correct output&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval _raw="host=foo DB1_DBNAME=foo_db DB1_DBVERSION=9.6.0 DB1_DBENV=DEV"
| kv
| fillnull DBNAME, DBENV, DBVERSION
| foreach DB*_DBNAME [ eval DBNAME=mvzip(DBNAME, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
| foreach DB*_DBENV [ eval DBENV=mvzip(DBENV, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
| foreach DB*_DBVERSION [ eval DBVERSION=mvzip(DBVERSION, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
| makemv delim="," DBNAME
| makemv delim="," DBENV
| makemv delim="," DBVERSION
| eval temp=mvzip(DBNAME,DBENV, ";"), temp1=mvzip(temp,DBVERSION, ";")
| rex mode=sed field=temp1 "s/(?:\d\;)+\d//g"
| mvexpand temp1
| where temp1!=""
| eval DB_DBNAME=mvindex(split(temp1,";"),0), DB_DBENV=mvindex(split(temp1,";"),1), DB_DBVERSION=mvindex(split(temp1,";"),2)
| table host, DB_DBNAME, DB_DBENV, DB_DBVERSION
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 29 Sep 2020 21:49:16 GMT</pubDate>
    <dc:creator>harsmarvania57</dc:creator>
    <dc:date>2020-09-29T21:49:16Z</dc:date>
    <item>
      <title>How do I merge a variable number of fields and expand rows?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444384#M126022</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;

&lt;P&gt;I have a dataset with single line events that contains a variable number of fields.  The number of fields is determined by the number of databases that exists in one host.  &lt;/P&gt;

&lt;P&gt;Here is an example of a host with two databases, though there could be many more than 2 databases on one host — up to 30 or even more.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;host=foo  DB1_DBNAME=foo_db  DB1_DBVERSION=9.6.0 DB1_DBENV=DEV  DB2_DBNAME=bar_db  DB2_DBVERSION=9.6.2 DB2_DBENV=DEV 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I'm looking to create a search that returns a result set that lists multiple rows for the host with each row having only the results for a single database — so, in the example above, something like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;host            DB_DBNAME    DB_DBVERSION   DB_DBENV  
foo             foo_db               9.6.0                       DEV                
foo             bar_db               9.6.2                      DEV
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 24 Oct 2018 11:05:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444384#M126022</guid>
      <dc:creator>skelly99</dc:creator>
      <dc:date>2018-10-24T11:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge a variable number of fields and expand rows?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444385#M126023</link>
      <description>&lt;P&gt;Hi @skelly99,&lt;/P&gt;

&lt;P&gt;Try below query&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;yourBaseSearch&amp;gt;
| eval TEMP_DB_DBNAME=mvzip(DB1_DBNAME, DB2_DBNAME), TEMP_DB_DBENV=mvzip(DB1_DBENV, DB2_DBENV), TEMP_DB_DBVERSION=mvzip(DB1_DBVERSION,DB2_DBVERSION)
| makemv delim="," TEMP_DB_DBNAME
| makemv delim="," TEMP_DB_DBENV
| makemv delim="," TEMP_DB_DBVERSION
| eval temp=mvzip(TEMP_DB_DBNAME,TEMP_DB_DBENV, ";"), temp1=mvzip(temp,TEMP_DB_DBVERSION, ";")
| mvexpand temp1
| eval DB_DBNAME=mvindex(split(temp1,";"),0), DB_DBENV=mvindex(split(temp1,";"),1), DB_DBVERSION=mvindex(split(temp1,";"),2)
| table host, DB_DBNAME, DB_DBENV, DB_DBVERSION
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is run anywhere search&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval _raw="host=foo DB1_DBNAME=foo_db DB1_DBVERSION=9.6.0 DB1_DBENV=DEV DB2_DBNAME=bar_db DB2_DBVERSION=9.6.2 DB2_DBENV=DEV"
| kv
| eval TEMP_DB_DBNAME=mvzip(DB1_DBNAME, DB2_DBNAME), TEMP_DB_DBENV=mvzip(DB1_DBENV, DB2_DBENV), TEMP_DB_DBVERSION=mvzip(DB1_DBVERSION,DB2_DBVERSION)
| makemv delim="," TEMP_DB_DBNAME
| makemv delim="," TEMP_DB_DBENV
| makemv delim="," TEMP_DB_DBVERSION
| eval temp=mvzip(TEMP_DB_DBNAME,TEMP_DB_DBENV, ";"), temp1=mvzip(temp,TEMP_DB_DBVERSION, ";")
| mvexpand temp1
| eval DB_DBNAME=mvindex(split(temp1,";"),0), DB_DBENV=mvindex(split(temp1,";"),1), DB_DBVERSION=mvindex(split(temp1,";"),2)
| table host, DB_DBNAME, DB_DBENV, DB_DBVERSION
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Oct 2018 13:16:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444385#M126023</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2018-10-24T13:16:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge a variable number of fields and expand rows?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444386#M126024</link>
      <description>&lt;P&gt;Thanks @harsmarvania57 - I can see that solution works however my one concern is how to expand this when we have lots of databases returned on a single host&lt;/P&gt;

&lt;P&gt;I don't know at this stage what the maximum will be but I know there are 10 or more is common and I've seen up to 30 databases.&lt;/P&gt;

&lt;P&gt;Wondering if there is there a way we can determine the number of DB related field and loop around this value to create the multi-value TEMP_DB* fields?&lt;/P&gt;</description>
      <pubDate>Wed, 24 Oct 2018 13:36:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444386#M126024</guid>
      <dc:creator>skelly99</dc:creator>
      <dc:date>2018-10-24T13:36:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge a variable number of fields and expand rows?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444387#M126025</link>
      <description>&lt;P&gt;Can you please below query for multiple DB&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;yourBaseSearch&amp;gt;
| fillnull DBNAME, DBENV, DBVERSION
| foreach DB*_DBNAME [ eval DBNAME=mvzip(DBNAME, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;) ]
| foreach DB*_DBENV [ eval DBENV=mvzip(DBENV, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;) ]
| foreach DB*_DBVERSION [ eval DBVERSION=mvzip(DBVERSION, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;) ]
| makemv delim="," DBNAME
| makemv delim="," DBENV
| makemv delim="," DBVERSION
| eval temp=mvzip(DBNAME,DBENV, ";"), temp1=mvzip(temp,DBVERSION, ";")
| rex mode=sed field=temp1 "s/(?:\d\;)+\d//g"
| mvexpand temp1
| where temp1!=""
| eval DB_DBNAME=mvindex(split(temp1,";"),0), DB_DBENV=mvindex(split(temp1,";"),1), DB_DBVERSION=mvindex(split(temp1,";"),2)
| table host, DB_DBNAME, DB_DBENV, DB_DBVERSION
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Below is run anywhere search&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval _raw="host=foo DB1_DBNAME=foo_db DB1_DBVERSION=9.6.0 DB1_DBENV=DEV DB2_DBNAME=bar_db DB2_DBVERSION=9.6.2 DB2_DBENV=DEV DB3_DBNAME=test_db DB3_DBVERSION=9.6.3 DB3_DBENV=PROD"
| kv
| fillnull DBNAME, DBENV, DBVERSION
| foreach DB*_DBNAME [ eval DBNAME=mvzip(DBNAME, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;) ]
| foreach DB*_DBENV [ eval DBENV=mvzip(DBENV, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;) ]
| foreach DB*_DBVERSION [ eval DBVERSION=mvzip(DBVERSION, &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;) ]
| makemv delim="," DBNAME
| makemv delim="," DBENV
| makemv delim="," DBVERSION
| eval temp=mvzip(DBNAME,DBENV, ";"), temp1=mvzip(temp,DBVERSION, ";")
| rex mode=sed field=temp1 "s/(?:\d\;)+\d//g"
| mvexpand temp1
| where temp1!=""
| eval DB_DBNAME=mvindex(split(temp1,";"),0), DB_DBENV=mvindex(split(temp1,";"),1), DB_DBVERSION=mvindex(split(temp1,";"),2)
| table host, DB_DBNAME, DB_DBENV, DB_DBVERSION
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Oct 2018 15:27:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444387#M126025</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2018-10-25T15:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge a variable number of fields and expand rows?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444388#M126026</link>
      <description>&lt;P&gt;Hi - thanks again for the post - the search works fine when there are two or databases on a host but when only one database is present nothing gets reported.  &lt;/P&gt;

&lt;P&gt;What I can see is that when an event only contains DB1_DBNAME, DB1_DBENV,  DB1_DBVERSION the foreach statement clobbers the field set with the fillnull statement&lt;/P&gt;

&lt;P&gt;The foreach statement works if no wildcard is used, ie foreach DB1_DBNAME but when DB*_DBNAME is used then the DBNAME field is no longer present.  The behaviour is different when a wildcard is used and DB1_DBNAME and DB2_DBNAME are in the event.&lt;/P&gt;

&lt;P&gt;To make this more intriguing is your run anywhere search works when I curtail the eval statement to read &lt;BR /&gt;
eval _raw="host=foo DB1_DBNAME=foo_db DB1_DBVERSION=9.6.0 DB1_DBENV=DEV&lt;/P&gt;

&lt;P&gt;Still head scratching - any suggestions welcome&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 21:45:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444388#M126026</guid>
      <dc:creator>skelly99</dc:creator>
      <dc:date>2020-09-29T21:45:56Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge a variable number of fields and expand rows?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444389#M126027</link>
      <description>&lt;P&gt;It is strange that it is not working for you with single DB, can you please replace &lt;CODE&gt;&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;&lt;/CODE&gt; with &lt;CODE&gt;'&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'&lt;/CODE&gt; in all 3 &lt;CODE&gt;foreach&lt;/CODE&gt; command ? &lt;CODE&gt;foreach&lt;/CODE&gt; command repeat &lt;CODE&gt;eval&lt;/CODE&gt; for every DB*_DBNAME so if you have only DB1_DBNAME then it will do &lt;CODE&gt;eval&lt;/CODE&gt; only once and zip &lt;CODE&gt;DBNAME (which is 0)&lt;/CODE&gt; and &lt;CODE&gt;DB1_DBNAME&lt;/CODE&gt;.  &lt;/P&gt;

&lt;P&gt;Search query which I have provided is working with 1 Database as well, I have tested below query in Splunk 7.1.2 and it is generating output.&lt;/P&gt;

&lt;P&gt;Below run anywhere search I am running and it is generating correct output&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval _raw="host=foo DB1_DBNAME=foo_db DB1_DBVERSION=9.6.0 DB1_DBENV=DEV"
| kv
| fillnull DBNAME, DBENV, DBVERSION
| foreach DB*_DBNAME [ eval DBNAME=mvzip(DBNAME, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
| foreach DB*_DBENV [ eval DBENV=mvzip(DBENV, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
| foreach DB*_DBVERSION [ eval DBVERSION=mvzip(DBVERSION, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
| makemv delim="," DBNAME
| makemv delim="," DBENV
| makemv delim="," DBVERSION
| eval temp=mvzip(DBNAME,DBENV, ";"), temp1=mvzip(temp,DBVERSION, ";")
| rex mode=sed field=temp1 "s/(?:\d\;)+\d//g"
| mvexpand temp1
| where temp1!=""
| eval DB_DBNAME=mvindex(split(temp1,";"),0), DB_DBENV=mvindex(split(temp1,";"),1), DB_DBVERSION=mvindex(split(temp1,";"),2)
| table host, DB_DBNAME, DB_DBENV, DB_DBVERSION
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 21:49:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444389#M126027</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2020-09-29T21:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge a variable number of fields and expand rows?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444390#M126028</link>
      <description>&lt;P&gt;Thanks again for your help - I've now managed to get this working using your logic but what I found in my data set is that the foreach DB* loop would only work if there were two fields&lt;/P&gt;

&lt;P&gt;So if DB1_DBNAME and DB2_DBNAME had values it worked - if there was only DB1_DBNAME then the foreach statement failed - as an example &lt;/P&gt;

&lt;P&gt;assume DB1_DBNAME=foo is set in the event&lt;BR /&gt;
&lt;EM&gt;fullnull DBNAME&lt;BR /&gt;
| foreach DB&lt;/EM&gt;_DBNAME [ eval DBNAME=mvzip(DBNAME, '&amp;lt;&amp;gt;') ]* &lt;BR /&gt;
the result here would be that the DBNAME field no longer existed&lt;/P&gt;

&lt;P&gt;However &lt;BR /&gt;
assume DB1_DBNAME=foo set in the event&lt;BR /&gt;
&lt;EM&gt;fullnull DBNAME DB2_DBNAME&lt;BR /&gt;
| foreach DB&lt;/EM&gt;_DBNAME [ eval DBNAME=mvzip(DBNAME, '&amp;lt;&amp;gt;') ]* &lt;BR /&gt;
would result in DBNAME being set as 0,foo,0&lt;/P&gt;

&lt;P&gt;So I got this to work across all events by running a fillnull statement to create the extra fields required across the data set which allowed the foreach loop to work with only single DB events   &lt;/P&gt;

&lt;P&gt;Does not explain why your run anywhere search works by my data set does not.   I'm wondering if its something to do with the initial field extraction??&lt;BR /&gt;
The fields we are using in the data set are being extracted automatically as named value pairs&lt;BR /&gt;
That is in the feed we get DB1_DBNAME=foo DB1_DBVERSION=9.6.2......&lt;/P&gt;

&lt;P&gt;I notice in your run anywhere search that you run the kv command - should I be added anything to the source type definition to ensure correct KV pair extraction?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 21:52:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444390#M126028</guid>
      <dc:creator>skelly99</dc:creator>
      <dc:date>2020-09-29T21:52:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge a variable number of fields and expand rows?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444391#M126029</link>
      <description>&lt;P&gt;When you search your Base search without foreach are you getting &lt;CODE&gt;DB1_DBNAME&lt;/CODE&gt; field on left hand side in Interesting Fields ? If yes then can you please run below command and let me know what will be in &lt;CODE&gt;DBNAME&lt;/CODE&gt; field for single DB events?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;yourBaseSearch&amp;gt;
| fillnull DBNAME, DBENV, DBVERSION
| foreach DB*_DBNAME [ eval DBNAME=mvzip(DBNAME, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
| foreach DB*_DBENV [ eval DBENV=mvzip(DBENV, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
| foreach DB*_DBVERSION [ eval DBVERSION=mvzip(DBVERSION, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;For me in run anywhere search DBNAME field value is &lt;CODE&gt;0,foo_db&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Nov 2018 15:26:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-merge-a-variable-number-of-fields-and-expand-rows/m-p/444391#M126029</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2018-11-01T15:26:50Z</dc:date>
    </item>
  </channel>
</rss>

