Splunk Search

How to use field value from outer query in inputlookup?

Thulasinathan_M
Contributor

Hi,

I'm trying to find whether a lookup file is available or not. If yes, I want to use the same file, if not I want to use different file, so far with some helps, I've written below query, the eval fileName if condition is working fine, in the stats I could see the correct results(desired files I'm looking for). 

But I'm wondering whether I could use the filename in makeresults and search for lookup file. Could someone please assist. Thanks in advance.

 

index=main sourcetype="dummySource"  events
| stats by EventCode
| append [ | inputlookup states.csv | stats count as isAvailable ]
| stats sum(isAvailable) as available, values(EventCode) as EventCode
| eval fileName = if(available > 0, "1.csv", "2.csv")
| stats values(available) as available values(EventCode) as EventCode by fileName
| join type=left fileName 
   [| inputlookup [ | makeresults 
    | eval search=fileName
    | table search ]]

 

Labels (4)
Tags (2)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

| inputlookup geo_attr_us_states.csv
| stats count as isAvailable
| eval fileName = if(isAvailable > 0, "1.csv", "2.csv")
| eval lookup="| inputlookup ".fileName
| map search="| makeresults | map search="$lookup$

View solution in original post

Thulasinathan_M
Contributor

Thanks @ITWhisperer, glad to know the sub-search runs first.
The inner inputlookup gives me the correct fileNames, but when I try it with either of options neither worked, any suggestions on what I'm doing wrong please.

Option1:
| inputlookup [| makeresults
[| inputlookup geo_attr_us_states.csv
| stats count as isAvailable
| eval fileName = if(isAvailable > 0, "1.csv", "2.csv")
| table fileName]]

Option 2:
| inputlookup [| makeresults
[| inputlookup geo_attr_us_states.csv
| stats count as isAvailable
| eval fileName = if(isAvailable > 0, "1.csv", "2.csv")
| table fileName]
| return $fileName]

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

| inputlookup geo_attr_us_states.csv
| stats count as isAvailable
| eval fileName = if(isAvailable > 0, "1.csv", "2.csv")
| eval lookup="| inputlookup ".fileName
| map search="| makeresults | map search="$lookup$

Thulasinathan_M
Contributor

Hi @ITWhisperer ,

Sorry,  I misunderstood my existing flow and it's I've to add a field  'env' value from the main search. As I'm a newbie to splunk couldn't find a solution for this, could you please kindly assist.

index=main sourcetype=java ErrorCode=400 env=prod
| join type=left ErrorCode
[| inputlookup [| makeresults 
        | eval search="Errors".env.strftime(now(),"%m%d").".csv" 
        | table search]
| stats count as isAvailable
| eval fileName = if(isAvailable > 0, "Errors".env.strftime(now(),"%m%d").".csv", "Errors".env.strftime(relative_time(now(), "-1d"),"%m%d").".csv")
| eval lookup="| inputlookup ".fileName
| map search="| makeresults | map search="$lookup$]

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

From your main search, env=prod so why not just use that string in the lookup file name?

 

0 Karma

Thulasinathan_M
Contributor

Thanks, working now.!!!

0 Karma

Thulasinathan_M
Contributor

@ITWhisperer  Based on the source the env values get change, from the results I add a new field as 'env' using rex and then have to use the field value to differentiate the files specific to each env.

0 Karma

Thulasinathan_M
Contributor

Thank you, it did the trick 🙂

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Essentially, you can't pass values from the outer search to the inner search, this is because, in general, the inner search is executed before the outer search.

One exception to this is the map command. However, the search which is executed for each event, replaces the event with its results.

You may be able to use this by doing the test first and use inputlookup to load the relevant csv file, then append your main search as a subsearch, then use stats to join your result to event from the lookup.

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!

Build the Future of Agentic AI: Join the Splunk Agentic Ops Hackathon

AI is changing how teams investigate incidents, detect threats, automate workflows, and build intelligent ...

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...