<?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 get the first and last number of consecutive integers by search? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436364#M124379</link>
    <description>&lt;P&gt;Here is another much more complicated solution:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval raw="id=1 id=3 id=4 id=5 id=10 id=13 id=14 id=15" 
| makemv raw
| mvexpand raw
| rename raw AS _raw
| kv

| rename COMMENT AS "Everything above generates sample event data; everything below is your solution"
| eval id=printf("%06d",id)
| stats list(id) AS ids
| nomv ids
| map search="
| makeresults
| eval ids=$ids$
| eval id=$ids$
| makemv id
| chart count BY ids id
| fields - ids
| eval _answer=-1, _prev_val=-999
| foreach * [
   eval _last_answer_digit=replace(_answer, \"^.*?(\d+)$\", \"\1\"),
        &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; = \"&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;\",
        _answer = case(
           (_answer==-1),                     &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,
           (_prev_val + 1==&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;),        _answer,
           (_last_answer_digit == _prev_val), _answer . \",&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;\",
           true(),                            _answer . \"-\" . _prev_val . \",&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;\"),
       _prev_prev_val = _prev_val,
       _prev_val = \"&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;\" ]"
| eval _answer = _answer . if((_prev_val==(_prev_prev_val + 1)), "-", ",") . _prev_val
| table _answer
| rename _answer AS answer
| rex field=answer mode=sed "s/,0+/,/g s/-0+/-/g"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 11 Mar 2019 23:23:32 GMT</pubDate>
    <dc:creator>woodcock</dc:creator>
    <dc:date>2019-03-11T23:23:32Z</dc:date>
    <item>
      <title>How to get the first and last number of consecutive integers by search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436361#M124376</link>
      <description>&lt;P&gt;Hello Splunkers,&lt;/P&gt;

&lt;P&gt;Need your help on this.&lt;/P&gt;

&lt;P&gt;This is my query for testing:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| fields id 
| sort id 
| delta id AS deltaid 
| eval consecutive=if(deltaid=1,"consecutive","nonconsecutive")

id
1
3
4
5
10
13
14
15

Output in a new field should be like this:
1
3, 5
10
13, 15
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Mar 2019 03:38:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436361#M124376</guid>
      <dc:creator>Oracle</dc:creator>
      <dc:date>2019-03-11T03:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the first and last number of consecutive integers by search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436362#M124377</link>
      <description>&lt;P&gt;@Oracle ,&lt;/P&gt;

&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| fields id
| sort id
| delta id AS deltaid|eval flag=if(deltaid==1,0,1)| accum flag as group
| eventstats min(id) as min , max(id) as max by group
| eval result=if(min==max,min,min.",".max)|fields id,result
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you do not want result in all rows, you can replace &lt;CODE&gt;eventstats&lt;/CODE&gt; with &lt;CODE&gt;stats&lt;/CODE&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|delta id AS deltaid|eval flag=if(deltaid==1,0,1)| accum flag as group
|stats  min(id) as min , max(id) as max,values(id) as id by group
|eval result=if(min==max,min,min.",".max)|fields id,result
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Mar 2019 05:38:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436362#M124377</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2019-03-11T05:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the first and last number of consecutive integers by search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436363#M124378</link>
      <description>&lt;P&gt;Hello @renjith.nair &lt;/P&gt;

&lt;P&gt;Great, your search provided is working fine! Thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Mar 2019 05:47:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436363#M124378</guid>
      <dc:creator>Oracle</dc:creator>
      <dc:date>2019-03-11T05:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the first and last number of consecutive integers by search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436364#M124379</link>
      <description>&lt;P&gt;Here is another much more complicated solution:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval raw="id=1 id=3 id=4 id=5 id=10 id=13 id=14 id=15" 
| makemv raw
| mvexpand raw
| rename raw AS _raw
| kv

| rename COMMENT AS "Everything above generates sample event data; everything below is your solution"
| eval id=printf("%06d",id)
| stats list(id) AS ids
| nomv ids
| map search="
| makeresults
| eval ids=$ids$
| eval id=$ids$
| makemv id
| chart count BY ids id
| fields - ids
| eval _answer=-1, _prev_val=-999
| foreach * [
   eval _last_answer_digit=replace(_answer, \"^.*?(\d+)$\", \"\1\"),
        &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; = \"&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;\",
        _answer = case(
           (_answer==-1),                     &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,
           (_prev_val + 1==&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;),        _answer,
           (_last_answer_digit == _prev_val), _answer . \",&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;\",
           true(),                            _answer . \"-\" . _prev_val . \",&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;\"),
       _prev_prev_val = _prev_val,
       _prev_val = \"&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;\" ]"
| eval _answer = _answer . if((_prev_val==(_prev_prev_val + 1)), "-", ",") . _prev_val
| table _answer
| rename _answer AS answer
| rex field=answer mode=sed "s/,0+/,/g s/-0+/-/g"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Mar 2019 23:23:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436364#M124379</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2019-03-11T23:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the first and last number of consecutive integers by search?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436365#M124380</link>
      <description>&lt;P&gt;Wow; very, Very, VERY nicely done!  Take a look at my brute-force approach!!!&lt;/P&gt;</description>
      <pubDate>Mon, 11 Mar 2019 23:25:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-first-and-last-number-of-consecutive-integers-by/m-p/436365#M124380</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2019-03-11T23:25:20Z</dc:date>
    </item>
  </channel>
</rss>

