<?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: Find events either side of a matched event in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406391#M167932</link>
    <description>&lt;P&gt;This is a perfect use case for &lt;CODE&gt;streamstats&lt;/CODE&gt;, which passes the records in order and performs aggregate commands, including &lt;CODE&gt;last()&lt;/CODE&gt;.  When combined with &lt;CODE&gt;current=f&lt;/CODE&gt;, it can be used to copy information from the prior record,  in whatever order the records are coming in.  Streamstats supports the &lt;CODE&gt;by&lt;/CODE&gt; clause, and in this case you want &lt;CODE&gt;by SessionID&lt;/CODE&gt;.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|makeresults|eval mydata="12/08/2018 11:12:27,1,123,Y,a!!!!12/08/2018 11:12:28,1,123,N,b!!!!12/08/2018 11:12:29,2,789,Y,c!!!!12/08/2018 11:12:30,1,321,N,d!!!!12/08/2018 11:12:31,1,321,Y,e!!!!12/08/2018 11:12:32,2,987,N,f!!!!12/08/2018 11:12:33,1,123,N,g!!!!12/08/2018 11:12:34,1,321,N,h!!!!12/08/2018 11:12:35,2,987,N,i!!!!12/08/2018 11:12:36,1,321,N,j!!!!12/08/2018 11:12:37,1,321,N,k!!!!12/08/2018 11:12:38,2,987,Y,l!!!!12/08/2018 11:12:39,2,789,N,m!!!!12/08/2018 11:12:40,1,123,N,n!!!!12/08/2018 11:12:41,1,123,N,o!!!!12/08/2018 11:12:42,2,789,N,p!!!!12/08/2018 11:12:43,1,321,N,q!!!!12/08/2018 11:12:44,1,123,Y,r"|makemv delim="!!!!" mydata |mvexpand mydata | table mydata |rex field=mydata  "^(?&amp;lt;time&amp;gt;[^,]*),(?&amp;lt;SessionID&amp;gt;[^,]*),(?&amp;lt;UserID&amp;gt;[^,]*),(?&amp;lt;Match&amp;gt;[^,]*),(?&amp;lt;Data&amp;gt;[^,]*)$" |fields - mydata | eval _time = strptime(time,"%m/%d/%Y %H:%M:%S")| table _time SessionID UserID Match Data 
| sort 0 _time 
| rename COMMENT as "The above just inputs your data"

| rename COMMENT as "Now we copy info forward, then reverse the order and copy it backward"
| streamstats current=f last(Match) as lastMatch by SessionID 
| reverse 
| streamstats current=f last(Match) as nextMatch by SessionID 
| reverse 
| where Match="Y" OR lastMatch="Y" OR nextMatch="Y"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you wanted a slightly more complicated version that told the next and prior records WHAT RECORD they were being kept because of, then that could be done, too.  Just add more &lt;CODE&gt;last(fieldname) as lastfieldname&lt;/CODE&gt; and &lt;CODE&gt;last(fieldname) as nextfieldname&lt;/CODE&gt; clauses to the two &lt;CODE&gt;streamstats&lt;/CODE&gt; commands.&lt;/P&gt;</description>
    <pubDate>Sun, 12 Aug 2018 21:56:05 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2018-08-12T21:56:05Z</dc:date>
    <item>
      <title>Find events either side of a matched event</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406387#M167928</link>
      <description>&lt;P&gt;I am trying to find the best way to identify the event before and after a matched event for each SessionID&lt;/P&gt;

&lt;P&gt;Example data;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;time                | SessionID    | UserID | Match | Data
12/08/2018 11:12:27 | 1         | 123    | Y     | a
12/08/2018 11:12:28 | 1            | 123    | N     | b
12/08/2018 11:12:29 | 2         | 789    | Y     | c
12/08/2018 11:12:30 | 1         | 321    | N     | d
12/08/2018 11:12:31 | 1         | 321    | Y     | e
12/08/2018 11:12:32 | 2         | 987    | N     | f
12/08/2018 11:12:33 | 1         | 123    | N     | g
12/08/2018 11:12:34 | 1         | 321    | N     | h
12/08/2018 11:12:35 | 2         | 987    | N     | i
12/08/2018 11:12:36 | 1         | 321    | N     | j
12/08/2018 11:12:37 | 1         | 321    | N     | k
12/08/2018 11:12:38 | 2         | 987    | Y     | l
12/08/2018 11:12:39 | 2         | 789    | N     | m
12/08/2018 11:12:40 | 1         | 123    | N     | n
12/08/2018 11:12:41 | 1         | 123    | N     | o
12/08/2018 11:12:42 | 2         | 789    | N     | p
12/08/2018 11:12:43 | 1         | 321    | N     | q
12/08/2018 11:12:44 | 1         | 123    | Y     | r
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And the data i am trying to identify should look like this;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;time                | SessionID | UserID | Match | Data
12/08/2018 11:12:27 | 1      | 123    | Y     | a
12/08/2018 11:12:28 | 1         | 123    | N     | b
-------------------------------------------------------
12/08/2018 11:12:29 | 2      | 789    | Y     | c
12/08/2018 11:12:32 | 2      | 987    | N     | f
-------------------------------------------------------
12/08/2018 11:12:30 | 1      | 321    | N     | d
12/08/2018 11:12:31 | 1      | 321    | Y     | e
12/08/2018 11:12:33 | 1      | 123    | N     | g
-------------------------------------------------------
12/08/2018 11:12:35 | 2      | 987    | N     | i
12/08/2018 11:12:38 | 2      | 987    | Y     | l
12/08/2018 11:12:39 | 2      | 789    | N     | m
-------------------------------------------------------
12/08/2018 11:12:43 | 1      | 321    | N     | q
12/08/2018 11:12:44 | 1      | 123    | Y     | r
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Aug 2018 13:35:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406387#M167928</guid>
      <dc:creator>karlbosanquet</dc:creator>
      <dc:date>2018-08-12T13:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: Find events either side of a matched event</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406388#M167929</link>
      <description>&lt;P&gt;@karlbosanquet,just to understand your requirement, for the third section, on what basis are you selecting user id 123 for session 1? &lt;/P&gt;</description>
      <pubDate>Sun, 12 Aug 2018 13:53:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406388#M167929</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2018-08-12T13:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Find events either side of a matched event</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406389#M167930</link>
      <description>&lt;P&gt;It is the next event after a match for SessionID 1. &lt;/P&gt;</description>
      <pubDate>Sun, 12 Aug 2018 14:13:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406389#M167930</guid>
      <dc:creator>karlbosanquet</dc:creator>
      <dc:date>2018-08-12T14:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: Find events either side of a matched event</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406390#M167931</link>
      <description>&lt;P&gt;It would be good if the transaction command had the ability to look for a middle event, like startswith or endswith. Format could be something like this;&lt;/P&gt;

&lt;P&gt;Feature request EXAMPLE, not a real use of transaction!&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| transaction pivot=Match=Y maxbefore=1 maxafter=1 by SessionID
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Aug 2018 14:22:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406390#M167931</guid>
      <dc:creator>karlbosanquet</dc:creator>
      <dc:date>2018-08-12T14:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: Find events either side of a matched event</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406391#M167932</link>
      <description>&lt;P&gt;This is a perfect use case for &lt;CODE&gt;streamstats&lt;/CODE&gt;, which passes the records in order and performs aggregate commands, including &lt;CODE&gt;last()&lt;/CODE&gt;.  When combined with &lt;CODE&gt;current=f&lt;/CODE&gt;, it can be used to copy information from the prior record,  in whatever order the records are coming in.  Streamstats supports the &lt;CODE&gt;by&lt;/CODE&gt; clause, and in this case you want &lt;CODE&gt;by SessionID&lt;/CODE&gt;.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|makeresults|eval mydata="12/08/2018 11:12:27,1,123,Y,a!!!!12/08/2018 11:12:28,1,123,N,b!!!!12/08/2018 11:12:29,2,789,Y,c!!!!12/08/2018 11:12:30,1,321,N,d!!!!12/08/2018 11:12:31,1,321,Y,e!!!!12/08/2018 11:12:32,2,987,N,f!!!!12/08/2018 11:12:33,1,123,N,g!!!!12/08/2018 11:12:34,1,321,N,h!!!!12/08/2018 11:12:35,2,987,N,i!!!!12/08/2018 11:12:36,1,321,N,j!!!!12/08/2018 11:12:37,1,321,N,k!!!!12/08/2018 11:12:38,2,987,Y,l!!!!12/08/2018 11:12:39,2,789,N,m!!!!12/08/2018 11:12:40,1,123,N,n!!!!12/08/2018 11:12:41,1,123,N,o!!!!12/08/2018 11:12:42,2,789,N,p!!!!12/08/2018 11:12:43,1,321,N,q!!!!12/08/2018 11:12:44,1,123,Y,r"|makemv delim="!!!!" mydata |mvexpand mydata | table mydata |rex field=mydata  "^(?&amp;lt;time&amp;gt;[^,]*),(?&amp;lt;SessionID&amp;gt;[^,]*),(?&amp;lt;UserID&amp;gt;[^,]*),(?&amp;lt;Match&amp;gt;[^,]*),(?&amp;lt;Data&amp;gt;[^,]*)$" |fields - mydata | eval _time = strptime(time,"%m/%d/%Y %H:%M:%S")| table _time SessionID UserID Match Data 
| sort 0 _time 
| rename COMMENT as "The above just inputs your data"

| rename COMMENT as "Now we copy info forward, then reverse the order and copy it backward"
| streamstats current=f last(Match) as lastMatch by SessionID 
| reverse 
| streamstats current=f last(Match) as nextMatch by SessionID 
| reverse 
| where Match="Y" OR lastMatch="Y" OR nextMatch="Y"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you wanted a slightly more complicated version that told the next and prior records WHAT RECORD they were being kept because of, then that could be done, too.  Just add more &lt;CODE&gt;last(fieldname) as lastfieldname&lt;/CODE&gt; and &lt;CODE&gt;last(fieldname) as nextfieldname&lt;/CODE&gt; clauses to the two &lt;CODE&gt;streamstats&lt;/CODE&gt; commands.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Aug 2018 21:56:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Find-events-either-side-of-a-matched-event/m-p/406391#M167932</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2018-08-12T21:56:05Z</dc:date>
    </item>
  </channel>
</rss>

