<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to string compare field values in an eval macro? in Knowledge Management</title>
    <link>https://community.splunk.com/t5/Knowledge-Management/How-to-string-compare-field-values-in-an-eval-macro/m-p/418762#M3686</link>
    <description>&lt;P&gt;It didn't work for me unfortunately.&lt;/P&gt;

&lt;P&gt;The problem seems to be with what "$day$" evaluates to in the left side of the statement.&lt;BR /&gt;
If I make the macro:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;if(true(),"$day$","\"false\"")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then it returns the value of the field I pass it, but for some reason using "$day$" in the comparison doesn't work.&lt;/P&gt;</description>
    <pubDate>Tue, 04 Dec 2018 22:46:15 GMT</pubDate>
    <dc:creator>d389133</dc:creator>
    <dc:date>2018-12-04T22:46:15Z</dc:date>
    <item>
      <title>How to string compare field values in an eval macro?</title>
      <link>https://community.splunk.com/t5/Knowledge-Management/How-to-string-compare-field-values-in-an-eval-macro/m-p/418760#M3684</link>
      <description>&lt;P&gt;I'm working on a search to gather events from similar time periods over several weeks (ie: Mondays between 14:00 and 14:30)&lt;BR /&gt;
It's easy enough to do using eval statements after my initial search, but I'm trying to optimise by using eval macros in my search.&lt;/P&gt;

&lt;P&gt;In this case, I'm testing if the even is on the same weekday as the current day (ie: select all events that occurred on a Monday).&lt;/P&gt;

&lt;P&gt;Using normal evals I can do this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|sourcetype=*
| eval filter=if(date_wday==lower(strftime(time(),"%A")),date_wday,"drop")
| where date_wday=filter
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So converted to an eval macro, I've done this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;if("$day$"==lower(strftime(time(),"%A")),"\"$day$\"","\"drop\"")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, this returns false when I pass in a field, and no events are kept:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=*  date_wday=`FilterDay(date_wday)`
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Calling with a hardcoded string as the parameter does work (although its a little pointless):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=*  date_wday=`FilterDay("tuesday")`
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've tried every combination of quotes/escapes/tostring I can think of in the macro, and nothing has worked,, any suggestions?&lt;/P&gt;

&lt;P&gt;Note: The macro has been created in the webgui, and looks like this: &lt;BR /&gt;
&lt;IMG src="https://community.splunk.com/storage/temp/257771-macro.png" alt="alt text" /&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;EDIT&lt;/STRONG&gt;&lt;BR /&gt;
Something is really weird with how the macro treats the field I pass in.&lt;BR /&gt;
If I change the macro to this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;if("$day$"=="date_wday","$day$","\"drop\"")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It evaluates to true (meaning  there here "$day$" evaluates to the field name, not the field value) BUT what is returned is the value of date_wday (ie: wednesday, meaning that here "$day$" evaluates to the field value.)&lt;/P&gt;

&lt;P&gt;What the hell is causing the same expression to evaluate differently? How do I get the field value to be used in the if statement (I've also tried every other comparison function with the same results)&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;EDIT AGAIN&lt;/STRONG&gt;&lt;BR /&gt;
I was actually wrong, it never uses the value of $day$ inside the macro. Even the return actually returns date_wday as an unquoted string, which gets parsed as the field by the search when it returns, so doing date_wday=&lt;CODE&gt;FilterDay(date_wday)&lt;/CODE&gt; evaluates to date_wday=date_wday, and is always true.&lt;BR /&gt;
My question still stands, how can I get the macro to test the value of date_wday?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 22:15:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Knowledge-Management/How-to-string-compare-field-values-in-an-eval-macro/m-p/418760#M3684</guid>
      <dc:creator>d389133</dc:creator>
      <dc:date>2020-09-29T22:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to string compare field values in an eval macro?</title>
      <link>https://community.splunk.com/t5/Knowledge-Management/How-to-string-compare-field-values-in-an-eval-macro/m-p/418761#M3685</link>
      <description>&lt;P&gt;place " if(date_wday==lower(strftime(time(),"%A")),date_wday,"drop")" in macro defination and try, I think you don't want ro provide day(like tuesday) manually.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 22:15:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Knowledge-Management/How-to-string-compare-field-values-in-an-eval-macro/m-p/418761#M3685</guid>
      <dc:creator>nagarjuna280</dc:creator>
      <dc:date>2020-09-29T22:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to string compare field values in an eval macro?</title>
      <link>https://community.splunk.com/t5/Knowledge-Management/How-to-string-compare-field-values-in-an-eval-macro/m-p/418762#M3686</link>
      <description>&lt;P&gt;It didn't work for me unfortunately.&lt;/P&gt;

&lt;P&gt;The problem seems to be with what "$day$" evaluates to in the left side of the statement.&lt;BR /&gt;
If I make the macro:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;if(true(),"$day$","\"false\"")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then it returns the value of the field I pass it, but for some reason using "$day$" in the comparison doesn't work.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Dec 2018 22:46:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Knowledge-Management/How-to-string-compare-field-values-in-an-eval-macro/m-p/418762#M3686</guid>
      <dc:creator>d389133</dc:creator>
      <dc:date>2018-12-04T22:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to string compare field values in an eval macro?</title>
      <link>https://community.splunk.com/t5/Knowledge-Management/How-to-string-compare-field-values-in-an-eval-macro/m-p/418763#M3687</link>
      <description>&lt;P&gt;It is impossible to pass fields to eval macros.&lt;/P&gt;

&lt;P&gt;Macro expansion happens before the search is parsed. At macro expansion time, there are no fields! Let alone values assigned to fields. The search has not happened yet.&lt;/P&gt;

&lt;P&gt;To work around this, replace the eval macro with a regular macro that generates an eval.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Jan 2019 22:06:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Knowledge-Management/How-to-string-compare-field-values-in-an-eval-macro/m-p/418763#M3687</guid>
      <dc:creator>ruman_splunk</dc:creator>
      <dc:date>2019-01-22T22:06:07Z</dc:date>
    </item>
  </channel>
</rss>

