<?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: use of rex result in an eval, convert to number in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/use-of-rex-result-in-an-eval-convert-to-number/m-p/401810#M116272</link>
    <description>&lt;P&gt;Can you give sample event and the regex your are using to extract them? Try to use "\d+" in regex.&lt;/P&gt;</description>
    <pubDate>Thu, 17 May 2018 13:17:11 GMT</pubDate>
    <dc:creator>p_gurav</dc:creator>
    <dc:date>2018-05-17T13:17:11Z</dc:date>
    <item>
      <title>use of rex result in an eval, convert to number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/use-of-rex-result-in-an-eval-convert-to-number/m-p/401809#M116271</link>
      <description>&lt;P&gt;I have a long rex command that generates a bunch of fields, this works perfectly. In the left side field explorer in verbose mode, Splunk identifies the two fields as numbers with a # next to the field names, however executing an eval results in no result/null. If I do a string operation, I get the expected result. I tried this:&lt;/P&gt;

&lt;P&gt;|convert num(FieldA)|convert num(FieldB) |eval Result=FieldA+FieldB&lt;/P&gt;

&lt;P&gt;to add the two numbers together and the result I get looks like this:&lt;BR /&gt;
Field A: 12345&lt;BR /&gt;
Field B: 678&lt;BR /&gt;
Result: 12345 678&lt;BR /&gt;&lt;BR /&gt;
when I hoped for: 13023&lt;/P&gt;

&lt;P&gt;What am I missing here? &lt;/P&gt;</description>
      <pubDate>Thu, 17 May 2018 13:07:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/use-of-rex-result-in-an-eval-convert-to-number/m-p/401809#M116271</guid>
      <dc:creator>grantsmiley</dc:creator>
      <dc:date>2018-05-17T13:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: use of rex result in an eval, convert to number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/use-of-rex-result-in-an-eval-convert-to-number/m-p/401810#M116272</link>
      <description>&lt;P&gt;Can you give sample event and the regex your are using to extract them? Try to use "\d+" in regex.&lt;/P&gt;</description>
      <pubDate>Thu, 17 May 2018 13:17:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/use-of-rex-result-in-an-eval-convert-to-number/m-p/401810#M116272</guid>
      <dc:creator>p_gurav</dc:creator>
      <dc:date>2018-05-17T13:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: use of rex result in an eval, convert to number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/use-of-rex-result-in-an-eval-convert-to-number/m-p/401811#M116273</link>
      <description>&lt;P&gt;@grantsmiley would it be possible for you to share your rex and sample data on which rex is applied?&lt;/P&gt;

&lt;P&gt;Seems like your regular expression is adding whitespace character to either fieldA or fieldB or both. Hence they are treated as String. You should reevaluate/test your regular Expression on regex101.con to ensure that they are only extracting numeric part.&lt;/P&gt;

&lt;P&gt;Following is a run anywhere example with this kind of issue. I have used &lt;CODE&gt;trim()&lt;/CODE&gt; function to remove whitespace after fieldA. However, it is better to be resolved during field extraction using Regular Expression.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|  makeresults
|  eval _raw="fieldA=120 ;fieldB=130"
|  rex field=_raw "fieldA=(?&amp;lt;fieldA&amp;gt;[^\;]+)\;fieldB=(?&amp;lt;fieldB&amp;gt;.*)"
|  eval totalString=fieldA+fieldB
|  eval fieldA=trim(fieldA),fieldB=trim(fieldB)
|  eval totalNum=fieldA+fieldB
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;PS: You can definitely test your fieldA and fieldB using &lt;CODE&gt;trim()&lt;/CODE&gt; function to see whether they have whitespace character/s added to them.&lt;/P&gt;</description>
      <pubDate>Thu, 17 May 2018 13:22:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/use-of-rex-result-in-an-eval-convert-to-number/m-p/401811#M116273</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-05-17T13:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: use of rex result in an eval, convert to number</title>
      <link>https://community.splunk.com/t5/Splunk-Search/use-of-rex-result-in-an-eval-convert-to-number/m-p/401812#M116274</link>
      <description>&lt;P&gt;Yup, it was a character. The rex pulls fields out of a 4000 field wide fixed width line of text and is an enormous mess. I had to write a script to take the fixed width definition and write the rex command. It is about 3 screens tall in the query window in splunk. TRIM worked, now getting exactly what I want, I should have some up with that, and thought the convert to number would handle that, apparently not! Thanks !&lt;/P&gt;</description>
      <pubDate>Thu, 17 May 2018 13:34:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/use-of-rex-result-in-an-eval-convert-to-number/m-p/401812#M116274</guid>
      <dc:creator>grantsmiley</dc:creator>
      <dc:date>2018-05-17T13:34:35Z</dc:date>
    </item>
  </channel>
</rss>

