Splunk Search

How to write Splunk search different lookup depending on IF statement?

glennthechamp
Engager

Hi

I have a search that will display result that will fall under device1 and device2.

If device1 i need to check lookup1 (and if device2 then lookup2) for a match deviceName=device , outputting Code and doing a regex on Code to extract some values. The regex will be different for lookup1 and lookup2.

Here is my code that isnt working (no results displayed):

 

 

 

<base-search replaced some details with '...' for security>
<if device1>
| lookup lookup1 device as device output CODE
| mvexpand ...
| mvexpand ... 
| where ...!= device and like(..., "...%") 
| rename ... as ... 
| eval LRD1=substr(..., 1, 4), LRD2=substr(...,1,4)
<if device2>         
| lookup lookup2 device as device output CODE
| search Node=o* 
| rex field=Description "(?<bearer>...)" 
| table *

 

 

 

 

After each lookup there are a few operations to perform dependant on which lookup table is searched. Both searches are working ok on their own just not combined.

Thanks

Labels (1)
Tags (3)
0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @glennthechamp,

SPL is a search language  not a procedural language, so it isn't possible to create a code like the one you described.

You should change your approach and try something like this (it's a porkaround!)

 

<base-search replaced some details with '...' for security>
| lookup lookup1 device as device output CODE
| mvexpand ...
| mvexpand ... 
| where ...!= device and like(..., "...%") 
| rename field1 AS field1_device1 field2 AS field2_device2 field3 AS field3_device3
| eval LRD1=substr(..., 1, 4), LRD2=substr(...,1,4)
| append [ search <base-search replaced some details with '...' for security>
   | lookup lookup2 device as device output CODE
   | search Node=o* 
   | rex field=Description "(?<bearer>...)" 
   | rename field1 AS field1_device2 field2 AS field2_device2 field3 AS field3_device2
   | table * ]
| eval field1=if(device=device1,field1_device1,field1_device2), field2=if(device=device1,field2_device1,field2_device2), field3=if(device=device1,field3_device1,field3_device2)
| table field1 field2 field3

 

Otherwise, you have to use a JavaScript, but I cannot help you for this.

Ciao.

Giuseppe

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...