Splunk Search

Conditional Search String based on Time

syk19567
Explorer

Hi community,

 

I need to write a query which can adjust its search string based on event time. For example, if the event time is before 2024/01/01, events should include string "A" OR "B";

 

index="aws" sourcetype="dev" ("A" OR "B")

 

Else, events should include string "C" OR "D".

 

 

index="aws" sourcetype="dev" ("C" OR "D")

 

 

 

I have written this to get the search string, but have no idea how to make use of it.

 

index="aws" sourcetype="dev"
| eval search_string=if(_time < strptime("2024-01-01", "%Y-%m-%d"), "(\"A\" OR \"B\")", "(\"C\" OR \"D\")")
| search search_string

 

 

I've got a lot of help here, and really appreciate it!

Labels (3)
0 Karma

KendallW
Contributor

Hi @syk19567 would something like this work? (Replace timestamps with epoch time)

index="aws" sourcetype="dev" (earliest=-1y latest="2023/12/31 23:59:59" "A" OR "B") OR (earliest="2024/01/01 00:00:00" latest=now "C" OR "D") 



0 Karma

bowesmana
SplunkTrust
SplunkTrust

Couple of minor edits - latest is exclusive and format should be MM/DD/YYYY:HH:MM:SS, so it would be 

index="aws" sourcetype="dev" (earliest=-1y latest="01/01/2024:00:00:00" "A" OR "B") OR (earliest="01/01/2024:00:00:00" latest=now "C" OR "D") 

 

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!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...