Getting Data In

How can I access another field value in foreach?

cphair
Builder

I am collecting data from several sources into a lookup and would like to rename the various fields to indicate which source they came from--source1_name, source2_ip, and so on. Since I don't have control over how the original fields are named, I'd like to automatically rename them through foreach by prepending the sourcetype for each event to every fieldname in that event. However, I can't get it to work. I tried
foreach * [ rename <<FIELD>> as 'sourcetype'_<<FIELD>>]

but that takes the quoted string 'sourcetype' literally, rather than looking up the current event's value for sourcetype. Using angle brackets around sourcetype doesn't work either. Is it possible to automate this renaming, or am I going to have to handle each field manually?

0 Karma

adonio
Ultra Champion

hello there,

you can remove the single quotes:
your search ... | foreach * [ rename <<FIELD>> as sourcetype_<<FIELD>>]

i think that automating the filed names will be better done while bringing the data or in search time using props and transforms
although you mentioned you dont have access, i will recommend to contact your splunk admin and apply the correct sourcetype configurations and fields extractions to the data set in topic

hope it helps

0 Karma

cphair
Builder

That won't work. It just prepends the string sourcetype. I want the value of the sourcetype field.

0 Karma

adonio
Ultra Champion

can you elaborate a little?
how your fields look like today? how you would like them to look?
i guess i did not understand your question in full

0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...