<?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 force &amp;quot;eval&amp;quot; to cast an expression as numeric value? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17810#M2503</link>
    <description>&lt;P&gt;In 4.1.5, the eval command now includes a tonumber() function.&lt;/P&gt;

&lt;P&gt;For reference:
&lt;A href="http://www.splunk.com/base/Documentation/4.1.5/SearchReference/CommonEvalFunctions" rel="nofollow"&gt;http://www.splunk.com/base/Documentation/4.1.5/SearchReference/CommonEvalFunctions&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Sep 2010 00:51:54 GMT</pubDate>
    <dc:creator>Ellen</dc:creator>
    <dc:date>2010-09-16T00:51:54Z</dc:date>
    <item>
      <title>How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17801#M2494</link>
      <description>&lt;P&gt;Let's say you have two fields like so:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;a=0001L
b=0002L
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What's the best way to force the &lt;CODE&gt;eval&lt;/CODE&gt; command to see these as numeric fields so that you can add these two values together.  It seems like you should be able to simply say something like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval c=rtrim(a,"L") + rtrim(b,"L")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, "c" ends up with the value "00010002" instead of 3 because within the scope of that individual &lt;CODE&gt;eval&lt;/CODE&gt; splunk thinks the output of both &lt;CODE&gt;rtrim&lt;/CODE&gt; as strings and not as numeric values.&lt;/P&gt;

&lt;P&gt;&lt;EM&gt;To quickly test this yourself, you can run this contrived search command:&lt;/EM&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;* | head 1 | a="0001L" | eval b="0002L" | eval c=rtrim(a,"L") + rtrim(b,"L") | fields a b c
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So the question is this:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;P&gt;&lt;STRONG&gt;Is there anyway to force &lt;CODE&gt;eval&lt;/CODE&gt; to cast the output of an expressions to a numeric value, so  that "+" becomes a mathematical operation and not a string concatenation?&lt;/STRONG&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;Yes, I realize that simply breaking this into multiple &lt;CODE&gt;eval&lt;/CODE&gt;s solves this problem, like so:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...  eval _a=rtrim(a,"L") | eval _b=rtrim(b,"L") | eval c= _a+_b
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, this does &lt;EM&gt;NOT&lt;/EM&gt; work for my use case.  I am attempting to write an eval-based macro expression which must be a single &lt;CODE&gt;eval&lt;/CODE&gt;, not a series of evals.  Therefore, all the work has to be done in a single &lt;CODE&gt;eval&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;I guess I'm looking for counterpart of &lt;CODE&gt;tostring&lt;/CODE&gt;.  Is there some an undocumented "tonumber()" or similar function?&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2010 06:30:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17801#M2494</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-07-21T06:30:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17802#M2495</link>
      <description>&lt;P&gt;Is there any way you can smuggle a convert num(a) in front of this for your macro?&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2010 21:40:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17802#M2495</guid>
      <dc:creator>ftk</dc:creator>
      <dc:date>2010-07-21T21:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17803#M2496</link>
      <description>&lt;P&gt;Nope.  It has to be within a single eval expression.   Since my macro is eval-based, I can't even do any kind of nested macro tricks.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2010 22:39:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17803#M2496</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-07-21T22:39:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17804#M2497</link>
      <description>&lt;P&gt;If the values really are all single digits with 0s prepended, I have (a very ugly) solution for you.  The one function that maps string to num, is &lt;CODE&gt;len&lt;/CODE&gt;.  Ready?  First, strip the &lt;CODE&gt;0&lt;/CODE&gt;s.  Then, replace &lt;CODE&gt;1&lt;/CODE&gt; with a single &lt;CODE&gt;x&lt;/CODE&gt;, &lt;CODE&gt;2&lt;/CODE&gt; with &lt;CODE&gt;xx&lt;/CODE&gt;, and so forth.  And finally, we can invoke &lt;CODE&gt;len&lt;/CODE&gt; on the resulting string (&lt;CODE&gt;xx&lt;/CODE&gt; maps to &lt;CODE&gt;2&lt;/CODE&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;* | head 1 | eval a="0001L" | eval b="0002L" | eval c=len(replace(replace(replace(rtrim(a,"L"), "0", ""), "1", "x"), "2", "xx")) + len(replace(replace(replace(rtrim(b,"L"), "0", ""), "1", "y"), "2", "yy")) | table a b c&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;gives&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  a     b   c
----- ----- -
0001L 0002L 3
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Jul 2010 13:36:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17804#M2497</guid>
      <dc:creator>V_at_Splunk</dc:creator>
      <dc:date>2010-07-22T13:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17805#M2498</link>
      <description>&lt;P&gt;Yeah, it looks like a tonumber() function is a badly needed addition to the language.  I'm going to file a bug report right now.&lt;/P&gt;

&lt;P&gt;The one (gross) workaround that I can think of is to abuse the strptime() time parser.  This would only work on non-negative integers, but it might be enough for your immediate issue:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$ splunk search '* | head 1 | eval a="0001L" | eval b="0002L" | eval c=strptime(rtrim(a,"L"),"%s") + strptime(rtrim(b,"L"),"%s") | table a b c'
  a     b      c
----- ----- --------
0001L 0002L 3.000000
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Jul 2010 14:19:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17805#M2498</guid>
      <dc:creator>mitch</dc:creator>
      <dc:date>2010-07-22T14:19:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17806#M2499</link>
      <description>&lt;P&gt;I agree -- badly needed.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jul 2010 19:41:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17806#M2499</guid>
      <dc:creator>ftk</dc:creator>
      <dc:date>2010-07-22T19:41:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17807#M2500</link>
      <description>&lt;P&gt;Wow.  That's hilarious.  But no, in my particular use case I'm looking at a time field.  So mapping 0-23 and 0-59 within a single eval using this approach would be quite ugly.  (I'd have to write a script just to generate my eval command.) Then I'd probably run into some eval expression character limit...  I found a 4.1-based workaround using regex-based &lt;CODE&gt;replace()&lt;/CODE&gt; and &lt;CODE&gt;relative_time()&lt;/CODE&gt;, which you can see here (if your interested):  &lt;A href="http://answers.splunk.com/questions/4528/finding-your-local-timezone-with-eval"&gt;http://answers.splunk.com/questions/4528/finding-your-local-timezone-with-eval&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jul 2010 22:31:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17807#M2500</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-07-22T22:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17808#M2501</link>
      <description>&lt;P&gt;Thanks for filing a request.  I found a workaround for my specific needs using &lt;CODE&gt;relative_time()&lt;/CODE&gt;.  But there are certainly other cases where such a solution wouldn't work, so a &lt;CODE&gt;tonumber()&lt;/CODE&gt;, or whatever it ends up being called would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jul 2010 22:36:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17808#M2501</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-07-22T22:36:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17809#M2502</link>
      <description>&lt;P&gt;+1.  Just for the craziness of this solution.  (I do wonder how far this would stretch.  Is the eval handled in a stack-based way, would you eventual end up with a stack overflow?...)&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jul 2010 22:38:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17809#M2502</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-07-22T22:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to force "eval" to cast an expression as numeric value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17810#M2503</link>
      <description>&lt;P&gt;In 4.1.5, the eval command now includes a tonumber() function.&lt;/P&gt;

&lt;P&gt;For reference:
&lt;A href="http://www.splunk.com/base/Documentation/4.1.5/SearchReference/CommonEvalFunctions" rel="nofollow"&gt;http://www.splunk.com/base/Documentation/4.1.5/SearchReference/CommonEvalFunctions&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2010 00:51:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-force-quot-eval-quot-to-cast-an-expression-as-numeric/m-p/17810#M2503</guid>
      <dc:creator>Ellen</dc:creator>
      <dc:date>2010-09-16T00:51:54Z</dc:date>
    </item>
  </channel>
</rss>

