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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...