Splunk Search

Splunk lookup using csv-keys as input and csv-values as output

bhupalbobbadi
Path Finder

I have event data as follows: a,b,",1,2,3,",c,d

And I have lookup table as follows

key,     value
1,       one
2,       two
3,       three
4,       four

I need the following output using lookup
a,b,",one,two,three,",c,d

note ",1,2,3," is not fixed in size.

Any help appreciated.

0 Karma
1 Solution

DalJeanis
Legend

Try this...

| makeresults 
| eval field1="a" | eval field2="b" | eval  field3=",1,2,3," | eval field4="c" | eval field5="d"
| rename COMMENT as "The above enters your test data"

| rename COMMENT as "Deal with the leading comma, mark spots to split the data, split the data into separate records"
| rex mode=sed field=field3 "s/^,/((nothing)),/g s/,/,!!!!/g s/!!!!$//g"
| makemv delim="!!!!" field3
| mvexpand field3

| rename COMMENT as "Do the lookup, add a comma if found"
| lookup mylookup.csv field3 OUTPUT field3b
| eval field3=if(isnull(field3b),field3,field3b.",")
| fields - field3b
| rename COMMENT as "For run anywhere you can just delete the lookup to prove it works"

| rename COMMENT as "Recombine the records, join them, undo the fix for the leading comma, voila"
| mvcombine field3
| eval field3=mvjoin(field3,"!!!!")
| rex mode=sed field=field3 "s/\(\(nothing\)\),/,/g s/,!!!!/,/g"

View solution in original post

DalJeanis
Legend

Try this...

| makeresults 
| eval field1="a" | eval field2="b" | eval  field3=",1,2,3," | eval field4="c" | eval field5="d"
| rename COMMENT as "The above enters your test data"

| rename COMMENT as "Deal with the leading comma, mark spots to split the data, split the data into separate records"
| rex mode=sed field=field3 "s/^,/((nothing)),/g s/,/,!!!!/g s/!!!!$//g"
| makemv delim="!!!!" field3
| mvexpand field3

| rename COMMENT as "Do the lookup, add a comma if found"
| lookup mylookup.csv field3 OUTPUT field3b
| eval field3=if(isnull(field3b),field3,field3b.",")
| fields - field3b
| rename COMMENT as "For run anywhere you can just delete the lookup to prove it works"

| rename COMMENT as "Recombine the records, join them, undo the fix for the leading comma, voila"
| mvcombine field3
| eval field3=mvjoin(field3,"!!!!")
| rex mode=sed field=field3 "s/\(\(nothing\)\),/,/g s/,!!!!/,/g"

bhupalbobbadi
Path Finder

Thanks tons. It worked as I wanted.

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!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...