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!

Technical Workshop Series: Splunk Data Management and SPL2 | Register here!

Hey, Splunk Community! Ready to take your data management skills to the next level? Join us for a 3-part ...

Spotting Financial Fraud in the Haystack: A Guide to Behavioral Analytics with Splunk

In today's digital financial ecosystem, security teams face an unprecedented challenge. The sheer volume of ...

Solve Problems Faster with New, Smarter AI and Integrations in Splunk Observability

Solve Problems Faster with New, Smarter AI and Integrations in Splunk Observability As businesses scale ...