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!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...