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
Champion

@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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

What Is Splunk? Here’s What You Can Do with Splunk

Hey Splunk Community, we know you know Splunk. You likely leverage its unparalleled ability to ingest, index, ...

Level Up Your .conf25: Splunk Arcade Comes to Boston

With .conf25 right around the corner in Boston, there’s a lot to look forward to — inspiring keynotes, ...

Manual Instrumentation with Splunk Observability Cloud: How to Instrument Frontend ...

Although it might seem daunting, as we’ve seen in this series, manual instrumentation can be straightforward ...