Splunk Search

How to return string in macro after some logics

madhavanv
New Member

I have following eval based macro to return a string, in the end I am expecting macro to return something like "earliest=08/20/2022:18:39:14 latest=08/20/2022:18:55:14"
so that i can use it in search as follows. 

index=main org_name="cards-org" app_name="service-prod"
`search_range("2022-08-20 19:15:14.104",2)`| table _time msg

But I am getting below error.  Please help to understand what is wrong with this and how to achieve this.

"Error in 'SearchParser': The definition of macro 'search_range(2)' is expected to be an eval expression that returns a string."

Eval based macro definition as follows.

| makeresults
|eval Date="$daterange$"
| eval minutes=$seconds$
| eval formattedEarlyts = strftime((strptime(Date, "%Y-%m-%d %H:%M:%S.%3N") - (minutes * 60)),"%m/%d/%Y:%H:%M:%S")
| eval formattedLatestts = strftime((strptime(Date, "%Y-%m-%d %H:%M:%S.%3N") + (minutes * 60)),"%m/%d/%Y:%H:%M:%S")
| eval timerange= " earliest="+formattedEarlyts+" "+"latest="+formattedLatestts
| fields - Date minutes formattedEarlyts formattedLatestts
| eval case (1==1,timerange)

Labels (1)
0 Karma

yuanliu
SplunkTrust
SplunkTrust

The error is as it says, the macro can only contain an eval expression, not full search commands.  For example, you can put

$a$

in an eval-base macro, or

$a$ + $b$

or

"\"" . strftime((strptime($daterange$, "%Y-%m-%d %H:%M:%S.%3N") - $seconds$ * 60)),"%m/%d/%Y:%H:%M:%S") . "\""

in eval-based macro

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!

SOK it to Me: Top 3 Benefits of Using Splunk Operator on Kubernetes that’ll Make ...

    Thursday, July 9, 2026  |  11:00AM–12:00PM PDT Duration: 1 hour (includes Q&A) Managing can feel like a ...

Upgrade Prep for 10.4, Network Observability Deep Dives, and More from Splunk Lantern

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...

Splunk Developer Day announcements: AI agents, MCP tools, Forecasting, and Custom ...

Splunk Developer Day was packed with product and platform updates for developers building in the AI ...