<?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: Eval a string as an expression in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184612#M53184</link>
    <description>&lt;P&gt;Please post one actual event so we can help with a real search expression.&lt;/P&gt;</description>
    <pubDate>Thu, 12 Mar 2015 14:31:17 GMT</pubDate>
    <dc:creator>dmaislin_splunk</dc:creator>
    <dc:date>2015-03-12T14:31:17Z</dc:date>
    <item>
      <title>Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184611#M53183</link>
      <description>&lt;P&gt;I have a table that I want to extract an expression from.  The expression is quoted string with some fields in it.  is there a way I can eval on a string. Something like this would be in my table:&lt;BR /&gt;
"Apache host:".host." has ".count." errors"&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2015 13:50:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184611#M53183</guid>
      <dc:creator>fk319</dc:creator>
      <dc:date>2015-03-12T13:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184612#M53184</link>
      <description>&lt;P&gt;Please post one actual event so we can help with a real search expression.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2015 14:31:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184612#M53184</guid>
      <dc:creator>dmaislin_splunk</dc:creator>
      <dc:date>2015-03-12T14:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184613#M53185</link>
      <description>&lt;P&gt;"Apache host:".host." has ".count." errors"&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2015 14:39:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184613#M53185</guid>
      <dc:creator>fk319</dc:creator>
      <dc:date>2015-03-12T14:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184614#M53186</link>
      <description>&lt;P&gt;Oh come on now Frank, fill in the blanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2015 14:46:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184614#M53186</guid>
      <dc:creator>dmaislin_splunk</dc:creator>
      <dc:date>2015-03-12T14:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184615#M53187</link>
      <description>&lt;P&gt;that is about it, my table will have something like:&lt;/P&gt;

&lt;P&gt;eventtype,output&lt;BR /&gt;
Apache,"""Apache host:"".host."" has "".count."" errors"""&lt;/P&gt;

&lt;P&gt;I will do something like&lt;BR /&gt;
[inputcsv Alerts | fields eventtype] | lookup Alerts eventtype | eval out={output} | table out&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2015 15:13:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184615#M53187</guid>
      <dc:creator>fk319</dc:creator>
      <dc:date>2015-03-12T15:13:01Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184616#M53188</link>
      <description>&lt;P&gt;My question comes down to this,&lt;/P&gt;

&lt;P&gt;| eval str="1+2"&lt;BR /&gt;
| eval sum=eval(str)&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2015 16:13:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184616#M53188</guid>
      <dc:creator>fk319</dc:creator>
      <dc:date>2015-03-12T16:13:31Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184617#M53189</link>
      <description>&lt;P&gt;are you looking to do any operation, or would it always be sum?&lt;BR /&gt;
| eval str="1+2"&lt;BR /&gt;
| rex field=str "(?\d+)+(?&lt;B&gt;\d+)"&lt;BR /&gt;
| eval sum=A+B&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;identifying the operator would make it more complicated, but it could probably be done...&lt;BR /&gt;
&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2015 23:16:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184617#M53189</guid>
      <dc:creator>lasnow</dc:creator>
      <dc:date>2015-03-12T23:16:37Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184618#M53190</link>
      <description>&lt;P&gt;mostly it is a string concationation, "apache host: ".host&lt;BR /&gt;
While host is common, often they include custom field extractions&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2015 15:28:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184618#M53190</guid>
      <dc:creator>fk319</dc:creator>
      <dc:date>2015-03-13T15:28:13Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184619#M53191</link>
      <description>&lt;P&gt;Can you provide your full query you're/you'll use, along with how many no of rows you're going to get?&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2015 16:14:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184619#M53191</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2015-03-13T16:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184620#M53192</link>
      <description>&lt;P&gt;not sure i fully understand the question, but would you be looking to do something like this?&lt;BR /&gt;
... | stats count by host | eval verbose="Apache host ".host." has ".count." errors" | table verbose&lt;/P&gt;

&lt;P&gt;or if "Apache host hostname has count errors" is your input you could do&lt;BR /&gt;
... | rex "Apache host (?[^\s]+) has (?\d+) errors" | table hostname, count&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2015 16:34:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184620#M53192</guid>
      <dc:creator>lasnow</dc:creator>
      <dc:date>2015-03-13T16:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184621#M53193</link>
      <description>&lt;P&gt;I believe you have similar requirement as this user.&lt;BR /&gt;
&lt;A href="http://answers.splunk.com/answers/222645/how-to-set-fielda-to-valuefieldb-inline-search-rep.html"&gt;http://answers.splunk.com/answers/222645/how-to-set-fielda-to-valuefieldb-inline-search-rep.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;You can try the option that I suggested in above post.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2015 16:47:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184621#M53193</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2015-03-13T16:47:28Z</dc:date>
    </item>
    <item>
      <title>Re: Eval a string as an expression</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184622#M53194</link>
      <description>&lt;P&gt;I think this is a job for &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Return" target="_blank"&gt;return&lt;/A&gt;.&lt;/P&gt;

&lt;P&gt;Here's an example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal
| head 1
| eval sum = [ stats count | eval string = "1+2" | return $string]
| table sum
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here &lt;CODE&gt;return&lt;/CODE&gt; results in the in-place substitution of the whole expression &lt;CODE&gt;[ stats count | eval string = "1+2" | return $string]&lt;/CODE&gt; with the string &lt;CODE&gt;1+2&lt;/CODE&gt;, which then allows &lt;CODE&gt;eval&lt;/CODE&gt; to attribute the value 3 to the "sum" field.&lt;/P&gt;

&lt;P&gt;While this answers the initial query of sourcing an eval expression with a string from an external source, your use-case seems a more complex as it would require iteration over a result set, using different eval expressions based on a field in the result - "eventtype", if I'm not mistaken.&lt;/P&gt;

&lt;P&gt;I can think of two ways that you may be able to achieve this:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;Use map as an iterator over your result set&lt;/LI&gt;
&lt;LI&gt;Provided that there is low variance in the eval expressions that you want to apply, you might want to consider simply doing this in-line (or better yet, with an &lt;CODE&gt;EVAL-&lt;/CODE&gt; directive in props.conf) using a case() statement. If you have to map a large number of eventtypes to a small number of eval expressions, you'll probably want to introduce a field such as "output_format_type" in your lookup table mapping various eventtypes to the output format you would like. That "output_format_type" is what your eval case() statement would operate on to decide what output format is appropriate.&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Mon, 28 Sep 2020 19:13:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Eval-a-string-as-an-expression/m-p/184622#M53194</guid>
      <dc:creator>hexx</dc:creator>
      <dc:date>2020-09-28T19:13:43Z</dc:date>
    </item>
  </channel>
</rss>

