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!

Get Inspired! We’ve Got Validation that Your Hard Work is Paying Off

We love our Splunk Community and want you to feel inspired by all your hard work! Eric Fusilero, our VP of ...

What's New in Splunk Enterprise 9.4: Features to Power Your Digital Resilience

Hey Splunky People! We are excited to share the latest updates in Splunk Enterprise 9.4. In this release we ...

Take Your Breath Away with Splunk Risk-Based Alerting (RBA)

WATCH NOW!The Splunk Guide to Risk-Based Alerting is here to empower your SOC like never before. Join Haylee ...