<?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 eval and stats first() (for dummies) in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82677#M20991</link>
    <description>&lt;P&gt;&lt;CODE&gt;eval&lt;/CODE&gt; works on a per-event basis, so stats commands for operating on multiple events doesn't apply.&lt;/P&gt;

&lt;P&gt;Splunk should already have indexed your data so that it uses OStime as its internal timestamp as well. In that case, getting the most recent event could be achieved like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | head 1
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 25 Jun 2012 08:41:57 GMT</pubDate>
    <dc:creator>Ayn</dc:creator>
    <dc:date>2012-06-25T08:41:57Z</dc:date>
    <item>
      <title>how to use eval and stats first() (for dummies)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82676#M20990</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;Summary: how to get most recent vents for a given ID (for dummies)&lt;/P&gt;

&lt;P&gt;I have data in the following format:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# OStime OSip OSreg OSstatus
1340406378 10.34.245.50 AG "NO TM"
1340406378 10.34.245.64 AG "NO TM"
1340406378 10.34.245.65 AG "NO TM"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;CODE&gt;OStime&lt;/CODE&gt; (which is correctly recognized as a timestamp) is unique to a set of data - there are many &lt;CODE&gt;OStime&lt;/CODE&gt; entries in the index but I am only interested in the most recent one. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | stats first(OStime)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;correctly shows me the first instance of &lt;CODE&gt;OStime&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I then tried to do use it in an eval to get only data corresponding to this timestamp&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval mostrecent = stats first(OStime) | search OStime=mostrecent
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This fails with a &lt;EM&gt;"Error in 'eval' command: The operator at 'first(OStime)' is invalid."&lt;/EM&gt;&lt;/P&gt;

&lt;P&gt;Thans in davnce for pointing me what is wrong, I looked at the examples in the &lt;A href="http://docs.splunk.com/Documentation/Splunk/5.0/SearchReference/Eval"&gt;documentation for eval&lt;/A&gt;, they look like mine except that they work &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2012 08:19:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82676#M20990</guid>
      <dc:creator>wsw70</dc:creator>
      <dc:date>2012-06-25T08:19:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to use eval and stats first() (for dummies)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82677#M20991</link>
      <description>&lt;P&gt;&lt;CODE&gt;eval&lt;/CODE&gt; works on a per-event basis, so stats commands for operating on multiple events doesn't apply.&lt;/P&gt;

&lt;P&gt;Splunk should already have indexed your data so that it uses OStime as its internal timestamp as well. In that case, getting the most recent event could be achieved like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | head 1
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jun 2012 08:41:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82677#M20991</guid>
      <dc:creator>Ayn</dc:creator>
      <dc:date>2012-06-25T08:41:57Z</dc:date>
    </item>
    <item>
      <title>Re: how to use eval and stats first() (for dummies)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82678#M20992</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;

&lt;P&gt;I believe that there is a bit of confusion of concepts. &lt;CODE&gt;stats&lt;/CODE&gt; operates on the whole set of events returned from the base search, and in your case you want to extract a single value from that set. &lt;CODE&gt;eval&lt;/CODE&gt; creates a new field for all events returned in the search. Creating a new field called 'mostrecent' for all events is probably not what you intended. &lt;/P&gt;

&lt;P&gt;Also if you look more closely at the &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/CommonEvalFunctions"&gt;documentation&lt;/A&gt; for &lt;CODE&gt;eval&lt;/CODE&gt;, you will see that &lt;CODE&gt;stats&lt;/CODE&gt; is not a valid function to &lt;CODE&gt;eval&lt;/CODE&gt;. &lt;/P&gt;

&lt;P&gt;Solution:&lt;BR /&gt;
The default behaviour of Splunk is to return the most recent events first, so if you just want the find all events that have the same &lt;CODE&gt;OStime&lt;/CODE&gt; as the most recent event you can use the &lt;CODE&gt;head&lt;/CODE&gt; command in a subsearch;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=your_sourcetype [search sourcetype=your_sourcetype | head 1 | fields + OStime]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The subsearch (within the square brackets) returns the field OStime (along with its value) for the most recent event (head 1) and adds it to the outer search, so that the main search reads something like;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=your_sourcetype OStime=123345456
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope this helps,&lt;/P&gt;

&lt;P&gt;Kristian&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2012 08:56:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82678#M20992</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2012-06-25T08:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: how to use eval and stats first() (for dummies)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82679#M20993</link>
      <description>&lt;P&gt;Hmm, it seems that I spend too long time editing my answers &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2012 08:57:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82679#M20993</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2012-06-25T08:57:04Z</dc:date>
    </item>
    <item>
      <title>Re: how to use eval and stats first() (for dummies)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82680#M20994</link>
      <description>&lt;P&gt;Thanks for the very detailed answer. Everything is now clear.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2012 09:21:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82680#M20994</guid>
      <dc:creator>wsw70</dc:creator>
      <dc:date>2012-06-25T09:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: how to use eval and stats first() (for dummies)</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82681#M20995</link>
      <description>&lt;P&gt;Thanks! Together with Kristian's answer it is now clear&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jun 2012 09:22:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-use-eval-and-stats-first-for-dummies/m-p/82681#M20995</guid>
      <dc:creator>wsw70</dc:creator>
      <dc:date>2012-06-25T09:22:43Z</dc:date>
    </item>
  </channel>
</rss>

