<?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 use rex in Calculated Fields? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/270883#M81530</link>
    <description>&lt;P&gt;You can eval the results of extractions performed with rex, but you can't eval them in the rex itself (nor can you rex inside an eval).&lt;/P&gt;

&lt;P&gt;Try it in two steps.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex field=_raw "purple\s+widgets\s+(?&amp;lt;purplewidgets&amp;gt;\d+)\s+orange\s+widgets\s+(?&amp;lt;orangewidgets&amp;gt;\d+)" 
| eval totalwidgets=orangewidgets+purplewidgets
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Assuming you have an event like&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;somedatetime, purple widgets 5 orange widgets 3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What pops out the other end will be, among some other fluff,  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;orangewidgets   purplewidgets   totalwidgets
3               5               8 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you want to confirm, this is the exact, run-anywhere search you can test with.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| gentimes start="1/1/2016" end="1/2/2016" 
| eval myval="somedatetime, purple widgets 5 orange widgets 3"
| rex field=myval "purple\s+widgets\s+(?&amp;lt;purplewidgets&amp;gt;\d+)\s+orange\s+widgets\s+(?&amp;lt;orangewidgets&amp;gt;\d+)"
| eval totalwidgets=orangewidgets+purplewidgets
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;CODE&gt;Gentimes&lt;/CODE&gt; just  creates a single faked up "time" to work with.&lt;BR /&gt;
I create an "event" to work with with the first &lt;CODE&gt;eval&lt;/CODE&gt;.&lt;BR /&gt;
The &lt;CODE&gt;rex&lt;/CODE&gt; parses the field myval that I just created and extracts the digits for purple and orange widgets&lt;BR /&gt;
The last &lt;CODE&gt;eval&lt;/CODE&gt; does some math with them to find a total.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;If instead&lt;/STRONG&gt; you are trying to do a rex on a field you've created - well, as far as I know that works as long as it's a string and may work regardless.  You can see I do that with "myval" above, in fact!&lt;/P&gt;</description>
    <pubDate>Fri, 05 Feb 2016 20:26:47 GMT</pubDate>
    <dc:creator>Richfez</dc:creator>
    <dc:date>2016-02-05T20:26:47Z</dc:date>
    <item>
      <title>How to use rex in Calculated Fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/270880#M81527</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I'm new to this forum and Splunk in general, so thank you in advance for all your help.&lt;BR /&gt;
I'm trying to use &lt;CODE&gt;rex&lt;/CODE&gt; in Calculated fields to extract some text and then further format it.&lt;BR /&gt;
The rex itself looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex field=source(?\d{4}-\d{2}) 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but when I pass it along to the Eval Expression field, I'm getting the following error message:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Encountered the following error while trying to save: In handler 'props-eval': Invalid operator
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The regex works fine in Search.&lt;BR /&gt;
Any help will be much appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2016 14:59:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/270880#M81527</guid>
      <dc:creator>mmagnuson</dc:creator>
      <dc:date>2016-02-05T14:59:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to use rex in Calculated Fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/270881#M81528</link>
      <description>&lt;P&gt;The rex itself is a command, not a function that can be used within EVAL in search/ calculated fields. If you can provide some sample values and the formatting you're planning to do, we can suggest alternatives. My first guess will be the &lt;CODE&gt;replace&lt;/CODE&gt; function which comes with &lt;CODE&gt;eval&lt;/CODE&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2016 16:36:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/270881#M81528</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-02-05T16:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to use rex in Calculated Fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/270882#M81529</link>
      <description>&lt;P&gt;Thank you somesoni2. In this case I think I can figure something out using substrings, I  read somewhere it's possible to use rex with Calculated Fields, but I guess it's not &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2016 18:27:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/270882#M81529</guid>
      <dc:creator>mmagnuson</dc:creator>
      <dc:date>2016-02-05T18:27:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to use rex in Calculated Fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/270883#M81530</link>
      <description>&lt;P&gt;You can eval the results of extractions performed with rex, but you can't eval them in the rex itself (nor can you rex inside an eval).&lt;/P&gt;

&lt;P&gt;Try it in two steps.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex field=_raw "purple\s+widgets\s+(?&amp;lt;purplewidgets&amp;gt;\d+)\s+orange\s+widgets\s+(?&amp;lt;orangewidgets&amp;gt;\d+)" 
| eval totalwidgets=orangewidgets+purplewidgets
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Assuming you have an event like&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;somedatetime, purple widgets 5 orange widgets 3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What pops out the other end will be, among some other fluff,  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;orangewidgets   purplewidgets   totalwidgets
3               5               8 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you want to confirm, this is the exact, run-anywhere search you can test with.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| gentimes start="1/1/2016" end="1/2/2016" 
| eval myval="somedatetime, purple widgets 5 orange widgets 3"
| rex field=myval "purple\s+widgets\s+(?&amp;lt;purplewidgets&amp;gt;\d+)\s+orange\s+widgets\s+(?&amp;lt;orangewidgets&amp;gt;\d+)"
| eval totalwidgets=orangewidgets+purplewidgets
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;CODE&gt;Gentimes&lt;/CODE&gt; just  creates a single faked up "time" to work with.&lt;BR /&gt;
I create an "event" to work with with the first &lt;CODE&gt;eval&lt;/CODE&gt;.&lt;BR /&gt;
The &lt;CODE&gt;rex&lt;/CODE&gt; parses the field myval that I just created and extracts the digits for purple and orange widgets&lt;BR /&gt;
The last &lt;CODE&gt;eval&lt;/CODE&gt; does some math with them to find a total.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;If instead&lt;/STRONG&gt; you are trying to do a rex on a field you've created - well, as far as I know that works as long as it's a string and may work regardless.  You can see I do that with "myval" above, in fact!&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2016 20:26:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/270883#M81530</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2016-02-05T20:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to use rex in Calculated Fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/546417#M154907</link>
      <description>&lt;P&gt;Thank you. I was able to accomplish my goal using replace()&lt;/P&gt;</description>
      <pubDate>Thu, 01 Apr 2021 18:38:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-rex-in-Calculated-Fields/m-p/546417#M154907</guid>
      <dc:creator>swebb07g</dc:creator>
      <dc:date>2021-04-01T18:38:49Z</dc:date>
    </item>
  </channel>
</rss>

