Splunk Search

Eval macro with string argument

MatMeredith
Path Finder

I'm trying to define a Splunk eval based macro that takes a string as a parameter (where the string must be able to contain spaces), tests the value of the string, and then returns a value based on the outcome of that test. But I can't get it to work. Taking as a simplified example...

Macro definition:

if ($arg1$=="Test String", "true", "false")

Macro invocation:

`name("Test String")`

The macro always evaluates to false. I've tried every combination of single and double quotes that I can think of in both the macro definition and invocation, but I can't seem to get this to work. Am I missing something?

Thanks!

Tags (2)
0 Karma
1 Solution

martin_mueller
SplunkTrust
SplunkTrust

Define your macro as follows:

if("$arg1$"=="test","true","false") 

The quotes around $arg1$ are necessary. Then the value placed into the macro content by string substitution again gets treated as a string rather than as a reference to a field.

View solution in original post

martin_mueller
SplunkTrust
SplunkTrust

Define your macro as follows:

if("$arg1$"=="test","true","false") 

The quotes around $arg1$ are necessary. Then the value placed into the macro content by string substitution again gets treated as a string rather than as a reference to a field.

MatMeredith
Path Finder

Perfect -- thank you Martin.

0 Karma

linu1988
Champion

Hello Martin,
both the scenario works for me, just had to tweak the passing of arguments.

0 Karma

linu1988
Champion

Hello Mat,
please write the macro definiton as:

" if ($arg1$=="Test String", "true", "false")

and call the eval statement as "eval a=`macro_name((Backslash)\"Test String(Backslash)\")" as double quote will give you error

0 Karma
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!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...