Splunk Search

replace string (using map/object)

posix
Observer

hello,

is there anyway to define a map / object. IE { '123': 'something',  '1234', 'anotherThing' } and then replace strings with '123' with 'something' and strings with '1234' with 'anotherThing'?

Labels (1)
0 Karma

thambisetty
SplunkTrust
SplunkTrust

You can do that easily using rex mode=sed. but if you have very large number of replacements then rex would not be a right fit.

using rex if you have small number of replacements:

| makeresults 
| eval image_name="123_456_789_10"
| rex mode=sed "s/123/something/g" field=image_name
| rex mode=sed "s/456/something2/g" field=image_name
| rex mode=sed "s/789/something3/g" field=image_name
| rex mode=sed "s/10/something5/g" field=image_name

using kv-store lookup if you have very large number of replacements:

  1. you need to define kv-store lookup 
  2. you need to have two fields let's call it: number and text
  3. The kv-store lookup should look like below:

thambisetty_0-1606026508886.png

  1.  The final search looks like below:
| makeresults 
| eval image_name="123_456_789_10"
| makemv image_name delim="_"
| lookup test_mv_final number as image_name OUTPUT text as decode_image_name
| eventstats list(decode_image_name) as decode_image_name delim="_"
| mvcombine decode_image_name

 Reason for using kv-store lookup rather csv lookup is to perform lookup against multi value field. lookups are not aware of multivalue fields.

————————————
If this helps, give a like below.
0 Karma

posix
Observer

@thambisetty  thanks for the response. let me clarify the question.

 

i have a field called image_name which is a string. the string is like '123_456_789_10'. and i want to decode the string from an object. like this {123: 'something', 456: 'something2', 789: 'something3', 10: 'something5'}

 

and i wand to replace the values of the image_name field with the values of the object so the string will be like: something_something2_something3_something5.

hopefully this makes it clearer.

0 Karma

thambisetty
SplunkTrust
SplunkTrust

your question is not quite clear.

may be you find below is helpful.

|makeresults 
| eval object="{ '123': 'something',  '1234', 'anotherThing' }"
| rex mode=sed "s/'123'/'something'/g" field=object
| rex mode=sed "s/'1234'/'anotherThing'/g" field=object
————————————
If this helps, give a like below.
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Fuel Your Journey: What’s Waiting for You at the .conf26 Acceleration Station

Navigating the show floor at .conf26 isn't just about keynotes and technical breakout sessions; it's also ...

Join the Final Session of the Data Management & Federation Bootcamp Series

Over the past three sessions of the Data Management & Federation Bootcamp Series, we've explored how to build ...

From Data to Insight: Announcing the Winners of the Splunk Dashboard Contest

Hi Splunkers, First off, thank you to everyone who participated in our very first From Data to Insight: The ...