All Apps and Add-ons

How to Use variables in 'search' command?

ggranum
Engager

I am trying to do something in a rather complex search, but I believe I can map it down to the following.

I would like to use variable expansion or other (preferably simple) magic to recreate this query:


index=xyz severity=WARN ("This" OR "That")

So something like


index=xyz severity=WARN
| eval foo="This"
| eval bar="That"
| search ($foo$ OR $bar$)


There is a caveat that much later in the query I'd also need to filter on

A_FieldValue="*$foo$*"


( I am aware of the performance penalty of wildcard prefixes)

 

----

 

Possibly presenting a more specific (less contrived, but still contrived) example would help me find alternate answers:

 

How would one go about crafting a query to find log messages that contain the current e.g. year month day, as of the time of execution of the query?

index=xyz severity=WARN
| eval mentionsThisMonth=strftime(_time,"%Y.%m")
| search "$mentionsThisMonth$*

At this point I'm assuming I'll have to regex into a field and then compare the field to the calculated variable. Better (More performant, less memory and CPU hungry) solutions would be most welcome. 

 

Note: I am absolutely NOT interested in how to use date ranges. Which is all you find when you try to google anything to do with 'search' and 'date' as concepts together. I mean literally that there is a date-like thing in the raw log that isn't quite date-like enough to be automatically parsed out into a field.  



Thanks much,

Labels (1)
Tags (2)
0 Karma

ggranum
Engager

Please see the now-edited original post. 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try this

index=xyz severity=WARN [| makeresults | eval query=strftime(_time,"%Y.%m") | table query]
0 Karma

ggranum
Engager

Thank you, that's interesting info. Unfortunately after double checking on the performance limits of sub-searches, I am quite sure we're well outside the limits (one minute/10K events in result)

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

The subsearch shown only returns 1 event and it would do that very quickly. Are you referring to a different subsearch?

0 Karma

isoutamo
SplunkTrust
SplunkTrust

Hi

Splunk haven't variables on SPL.

If you really need this you need to use subquery on our 

index=xyz severity=WARN 
[ <your query returns "This" OR "That">]

but you couldn't use result of this again as A_FieldValue = ... You should write that subquery again with little bit different result.

But if you are doing this on dashboard then you probably could do it with tokens? Another option could be use map command, but usually it's not so efficient that it's good for anything bigger searches etc.

r. Ismo 

ITWhisperer
SplunkTrust
SplunkTrust

What do you mean by "variable expansion"?

Why can't you just do

index=xyz severity=WARN ("This" OR "That")
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!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...