Splunk Search

Create new fields using values from another field

bt149
Path Finder

I have a field called key. key has multivalues that are also dynamic. I have another field called values, that is also multivalued and dynamic. The values in "values" line-up with the values in "key".

Example:

keyvalues
AdditionalInfouser has removed device with id alpha_numeric_field" in area "alpha_numeric_field" for user "alpha_numeric_field".
DeviceIDalpha_numeric_field
DeviceTypemobile_device
OSWindows

 

Thanks in advance and I hope this makes sense.

I want to create a new field using the values from the field "key" and have the values be the values from "values".
The oucome would be:

AdditionalInfouser has removed device with id alpha_numeric_field" in area "alpha_numeric_field" for user "alpha_numeric_field".
DeviceIDalpha_numeric_field
DeviceTypemobile_device
OSWindows
Labels (1)
0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

First, I suspect that you meant the input looks like

key
values
AdditionalInfo
DeviceID
DeviceType
OS
user has removed device with id "alpha_numeric_field" in area "alpha_numeric_field" for user "alpha_numeric_field".
alpha_numeric_field
mobile_device
Windows

Second, I have a question about the origin of "key" and "values".  Could they come from a structure such as JSON?  Maybe there is a better opportunity than at the end of processing.

Third, I suspect that you meant "the output would be"

AdditionalInfoDeviceIDDeviceTypeOS
user has removed device with id "alpha_numeric_field" in area "alpha_numeric_field" for user "alpha_numeric_field".alpha_numeric_fieldmobile_deviceWindows

 

Finally, if your Splunk is 8.1 or later, you can use JSON functions and the multivalue mode of foreach to do the job:

| eval idx = mvrange(0, mvcount(key))
| eval keyvalue = json_object()
| foreach idx mode=multivalue
    [eval keyvalue = json_set(keyvalue, mvindex(key, <<ITEM>>), mvindex(values, <<ITEM>>))]
| spath input=keyvalue
| fields - idx key values keyvalue

Here is an emulation for you to play with and compare with real data

| makeresults format=csv data="key,values
AdditionalInfo,user has removed device with id \"alpha_numeric_field\" in area \"alpha_numeric_field\" for user \"alpha_numeric_field\".
DeviceID,alpha_numeric_field
DeviceType,mobile_device
OS,Windows"
| stats list(*) as *
``` data emulation above ```

View solution in original post

Tags (1)

yuanliu
SplunkTrust
SplunkTrust

First, I suspect that you meant the input looks like

key
values
AdditionalInfo
DeviceID
DeviceType
OS
user has removed device with id "alpha_numeric_field" in area "alpha_numeric_field" for user "alpha_numeric_field".
alpha_numeric_field
mobile_device
Windows

Second, I have a question about the origin of "key" and "values".  Could they come from a structure such as JSON?  Maybe there is a better opportunity than at the end of processing.

Third, I suspect that you meant "the output would be"

AdditionalInfoDeviceIDDeviceTypeOS
user has removed device with id "alpha_numeric_field" in area "alpha_numeric_field" for user "alpha_numeric_field".alpha_numeric_fieldmobile_deviceWindows

 

Finally, if your Splunk is 8.1 or later, you can use JSON functions and the multivalue mode of foreach to do the job:

| eval idx = mvrange(0, mvcount(key))
| eval keyvalue = json_object()
| foreach idx mode=multivalue
    [eval keyvalue = json_set(keyvalue, mvindex(key, <<ITEM>>), mvindex(values, <<ITEM>>))]
| spath input=keyvalue
| fields - idx key values keyvalue

Here is an emulation for you to play with and compare with real data

| makeresults format=csv data="key,values
AdditionalInfo,user has removed device with id \"alpha_numeric_field\" in area \"alpha_numeric_field\" for user \"alpha_numeric_field\".
DeviceID,alpha_numeric_field
DeviceType,mobile_device
OS,Windows"
| stats list(*) as *
``` data emulation above ```
Tags (1)

bt149
Path Finder

This looks very promising. Thank you for your valued input!

0 Karma

PickleRick
SplunkTrust
SplunkTrust

For creating fields dynamicaly you can use the {} syntax. Like

| makeresults
| eval field="field1",{field}="value"

But the important question and a possible issue here is where did you get the multivalued fields from. Remember that two distinct multivalued fields are... well, distinct. There is no relationship between their values whatsoever. And if you are creating multivalued field by means of list() or values() and the original data didn't have some values, you can't tel, which ones were empty. You're just getting a "squashed" list as a result.

0 Karma

kiran_panchavat
SplunkTrust
SplunkTrust

@bt149 

 

kiran_panchavat_0-1753111038556.png

 

Did this help? If yes, please consider giving kudos, marking it as the solution, or commenting for clarification — your feedback keeps the community going!
0 Karma

bt149
Path Finder

I appreciate the help, but this is not what I'm looking to do. I want to create the new fields so they could be used for searching. I already have a field using the mvzip command. Thanks again.

0 Karma
Get Updates on the Splunk Community!

CX Day is Coming!

Customer Experience (CX) Day is on October 7th!! We're so excited to bring back another day full of wonderful ...

Strengthen Your Future: A Look Back at Splunk 10 Innovations and .conf25 Highlights!

The Big One: Splunk 10 is Here!  The moment many of you have been waiting for has arrived! We are thrilled to ...

Now Offering the AI Assistant Usage Dashboard in Cloud Monitoring Console

Today, we’re excited to announce the release of a brand new AI assistant usage dashboard in Cloud Monitoring ...