<?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: Extract value by dynamic fields in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740952#M240505</link>
    <description>&lt;P&gt;Hi &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/308248"&gt;@alesyo&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think the JSON in my example shouldnt affect the outcome, as this was purely a way for me to provide a working example. You could use "fields" to list the fields you are interested in before running the foreach command?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=notable .etc...
| fields id interstingField1 interestingField2 ..etc..
| foreach * 
    [| eval summary=mvappend(summary,IF(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;!="" and "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"!="summary" and "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"!="id", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=".&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,null()))]
    | eval summary_output="Id:".id." - ".mvjoin(summary," ")
    | fields summary_output&lt;/LI-CODE&gt;&lt;P&gt;Please let me know how you get on and consider adding karma to this or any other answer if it has helped.&lt;BR /&gt;Regards&lt;/P&gt;&lt;P&gt;Will&lt;/P&gt;</description>
    <pubDate>Wed, 05 Mar 2025 17:27:55 GMT</pubDate>
    <dc:creator>livehybrid</dc:creator>
    <dc:date>2025-03-05T17:27:55Z</dc:date>
    <item>
      <title>Extract value by dynamic fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740931#M240500</link>
      <description>&lt;P&gt;Hi Community,&lt;BR /&gt;&lt;BR /&gt;I have the following challenge. I have different events, and for each event, I want to generate a summary with different values. These values are defined in a lookup table.&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;The following example:&lt;/STRONG&gt;&lt;BR /&gt;E1: id=1 , dest_ip=1.1.1.1, src_ip=2.2.2.2,.....&lt;BR /&gt;E2: id=2, user=bob,&amp;nbsp; domain=microsoft&lt;BR /&gt;E3: id=3 county=usa, city=seattle&lt;BR /&gt;E4: id=4 company=cisco, product=splunk&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Lookup Table: (Potential more fieldnames)&lt;/STRONG&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Field1&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;Field2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;dest_ip&lt;/TD&gt;&lt;TD&gt;src_ip&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;user&lt;/TD&gt;&lt;TD&gt;domain&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;country&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;company&lt;/TD&gt;&lt;TD&gt;product&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;Expected Output:&lt;/STRONG&gt;&lt;BR /&gt;id1: Summary dest_ip=1.1.1.1 src_ip=2.2.2.2&lt;BR /&gt;Id2: Summary user=bob domain=microsoft&lt;BR /&gt;id3: Summary country=usa&lt;BR /&gt;Id4: Summary company=splunk, product =splunk&lt;BR /&gt;&lt;BR /&gt;The solution could be using a case function but it doesn't scale well becuse I woult need to add a new line for each case. Potentially, the number of cases could grow to 1000.&lt;BR /&gt;I tried to solve with foreach, but I am unable to retrieve the values from the event.&lt;BR /&gt;&lt;BR /&gt;Here's the query I tried.&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;index=events
| lookup cases.csv id OUTPUT field1, field2
| foreach field*
[ eval summary = summary + "&amp;lt;&amp;lt;field&amp;gt;&amp;gt;" + ":" &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;
]
table id, summary&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;&lt;BR /&gt;Thanks for your help!&lt;BR /&gt;Alesyo&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Mar 2025 14:36:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740931#M240500</guid>
      <dc:creator>alesyo</dc:creator>
      <dc:date>2025-03-05T14:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: Extract value by dynamic fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740936#M240502</link>
      <description>&lt;LI-CODE lang="markup"&gt;| foreach *
    [| eval summary1=if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"==Field1,&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,summary1)
    | eval summary2=if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"==Field2,&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,summary2)]
| eval summary=Field1."=".summary1.if(isnotnull(Field2)," ".Field2."=".summary2,null())&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 05 Mar 2025 15:18:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740936#M240502</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2025-03-05T15:18:24Z</dc:date>
    </item>
    <item>
      <title>Re: Extract value by dynamic fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740938#M240503</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/308248"&gt;@alesyo&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How about this?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="livehybrid_0-1741188071125.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/38020i0D0CF6DE7933B4B4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="livehybrid_0-1741188071125.png" alt="livehybrid_0-1741188071125.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;You would just need to use this on your existing query I think&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| foreach * 
    [| eval summary=mvappend(summary,IF(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;!="" and "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"!="id", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=".&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,null()))]
    | eval summary_output="Id:".id." - ".mvjoin(summary," ")
    | fields summary_output&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However I've included a full working example below:&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 data="[{\"id\":1,\"dest_ip\":\"1.1.1.1\",\"src_ip\":\"2.2.2.2\"},{\"id\":2,\"user\":\"bob\",\"domain\":\"microsoft\"},{\"id\":3,\"county\":\"usa\",\"city\":\"seattle\"},{\"id\":4,\"company\":\"cisco\",\"product\":\"splunk\"}]"
| eval rawdata=json_array_to_mv(data)
| mvexpand rawdata
| eval _raw=json_extract(rawdata,"")
| fields - data rawdata
| spath
| stats values(*) AS * by id
| foreach * 
    [| eval summary=mvappend(summary,IF(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;!="" and "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"!="id", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=".&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,null()))]
    | eval summary_output="Id:".id." - ".mvjoin(summary," ")
    | fields summary_output&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know how you get on and consider adding karma to this or any other answer if it has helped.&lt;BR /&gt;Regards&lt;/P&gt;&lt;P&gt;Will&lt;/P&gt;</description>
      <pubDate>Wed, 05 Mar 2025 15:23:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740938#M240503</guid>
      <dc:creator>livehybrid</dc:creator>
      <dc:date>2025-03-05T15:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: Extract value by dynamic fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740940#M240504</link>
      <description>&lt;P&gt;Thanks for your answer,&lt;BR /&gt;&lt;BR /&gt;The events are part of an index, which aren't available as json. It is a shared notable index.&lt;BR /&gt;My idea is to define in a lookup which fieldnames I will extract.&lt;BR /&gt;&lt;BR /&gt;For Example:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval sum=case(id=1, "dest_ip:" .dest_ip ",src_ip:".src_ip,
 id=2, "user:".user + ",domain:".domain
 id=3, "country:".country,
 id=4, "company:".company + ",product:".product)
| table id, sum&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the scalable is very worse, because the kind of condition is grow up to 1000. I think is not manageable in one use case.&lt;BR /&gt;&lt;BR /&gt;Thanks for your help&lt;BR /&gt;Best regards&lt;BR /&gt;Tino&lt;/P&gt;</description>
      <pubDate>Wed, 05 Mar 2025 15:48:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740940#M240504</guid>
      <dc:creator>alesyo</dc:creator>
      <dc:date>2025-03-05T15:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: Extract value by dynamic fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740952#M240505</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/308248"&gt;@alesyo&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think the JSON in my example shouldnt affect the outcome, as this was purely a way for me to provide a working example. You could use "fields" to list the fields you are interested in before running the foreach command?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=notable .etc...
| fields id interstingField1 interestingField2 ..etc..
| foreach * 
    [| eval summary=mvappend(summary,IF(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;!="" and "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"!="summary" and "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"!="id", "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=".&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,null()))]
    | eval summary_output="Id:".id." - ".mvjoin(summary," ")
    | fields summary_output&lt;/LI-CODE&gt;&lt;P&gt;Please let me know how you get on and consider adding karma to this or any other answer if it has helped.&lt;BR /&gt;Regards&lt;/P&gt;&lt;P&gt;Will&lt;/P&gt;</description>
      <pubDate>Wed, 05 Mar 2025 17:27:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740952#M240505</guid>
      <dc:creator>livehybrid</dc:creator>
      <dc:date>2025-03-05T17:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Extract value by dynamic fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740996#M240515</link>
      <description>&lt;P&gt;The key to do this is to organize fields from events into an easy-to-access format after the lookup table. &amp;nbsp;Traditionally, Splunkers use mvjoin-split action. &amp;nbsp;But for a highly variable use case like this, it is almost impossible. &amp;nbsp;You want a structured data representation. &amp;nbsp;Something like, oh, I know, JSON.&lt;/P&gt;&lt;P&gt;If you use Splunk 8.1 or later, I recommend this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| tojson output_field=hash
| lookup cases.csv id
| foreach Field1 Field2
    [eval output = mvappend(output, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;' . "=" . json_extract(hash, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'))]
| eval output = "id" . id . " Summary " . mvjoin(output, " ")
| table output hash Field1 Field2&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This should work with any number of cases. &amp;nbsp;To illustrate the point, this comes from your sample data and sample lookup:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;output&lt;/TD&gt;&lt;TD&gt;hash&lt;/TD&gt;&lt;TD&gt;Field1&lt;/TD&gt;&lt;TD&gt;Field2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;id1 Summary src_ip=2.2.2.2 dest_ip=1.1.1.1&lt;/TD&gt;&lt;TD&gt;{"dest_ip":"1.1.1.1","id":1,"src_ip":"2.2.2.2"}&lt;/TD&gt;&lt;TD&gt;src_ip&lt;/TD&gt;&lt;TD&gt;dest_ip&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;id2 Summary user=bob domain=microsoft&lt;/TD&gt;&lt;TD&gt;{"domain":"microsoft","id":2,"user":"bob"}&lt;/TD&gt;&lt;TD&gt;user&lt;/TD&gt;&lt;TD&gt;domain&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;id3 Summary country=usa&lt;/TD&gt;&lt;TD&gt;{"city":"seattle","country":"usa","id":3}&lt;/TD&gt;&lt;TD&gt;country&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;id4 Summary company=cisco product=splunk&lt;/TD&gt;&lt;TD&gt;{"company":"cisco","id":4,"product":"splunk"}&lt;/TD&gt;&lt;TD&gt;company&lt;/TD&gt;&lt;TD&gt;product&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;(Interestingly, if you are pre-8.1, you can replace json_extract with spath - the function, not command, and the search still works in this case.)&lt;/P&gt;&lt;P&gt;Here is an emulation for you to play with and compare with real data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval data = split("E1: id=1 , dest_ip=1.1.1.1, src_ip=2.2.2.2,.....
E2: id=2, user=bob,  domain=microsoft
E3: id=3 country=usa, city=seattle
E4: id=4 company=cisco, product=splunk", "
")
| mvexpand data
| rename data as _raw
| extract
| fields - _*
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 06:11:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extract-value-by-dynamic-fields/m-p/740996#M240515</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2025-03-06T06:11:13Z</dc:date>
    </item>
  </channel>
</rss>

