<?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 How to include the previous and/or following event after the search string appears? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/126715#M34320</link>
    <description>&lt;P&gt;Hi,&lt;BR /&gt;
I would like to include the event just before or just after the search string appears. Basically like grep -A 1 or -B 1 would. Is there an easy way of doing this in Splunk?&lt;/P&gt;</description>
    <pubDate>Tue, 16 Sep 2014 15:55:56 GMT</pubDate>
    <dc:creator>echalex</dc:creator>
    <dc:date>2014-09-16T15:55:56Z</dc:date>
    <item>
      <title>How to include the previous and/or following event after the search string appears?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/126715#M34320</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
I would like to include the event just before or just after the search string appears. Basically like grep -A 1 or -B 1 would. Is there an easy way of doing this in Splunk?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 15:55:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/126715#M34320</guid>
      <dc:creator>echalex</dc:creator>
      <dc:date>2014-09-16T15:55:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to include the previous and/or following event after the search string appears?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/126716#M34321</link>
      <description>&lt;P&gt;Please check below post&lt;/P&gt;

&lt;P&gt;&lt;A href="http://answers.splunk.com/answers/150509/how-to-get-events-around-identified-event.html"&gt;http://answers.splunk.com/answers/150509/how-to-get-events-around-identified-event.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Sep 2014 16:02:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/126716#M34321</guid>
      <dc:creator>pradeepkumarg</dc:creator>
      <dc:date>2014-09-16T16:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to include the previous and/or following event after the search string appears?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/126717#M34322</link>
      <description>&lt;P&gt;Thanks for the tip.&lt;BR /&gt;
It isn't strictly what I was looking for, as it is time-based, rather than event based. So there can be quite a few events during the time I specify -even just a second, but at least it's there.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Sep 2014 11:19:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/126717#M34322</guid>
      <dc:creator>echalex</dc:creator>
      <dc:date>2014-09-18T11:19:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to include the previous and/or following event after the search string appears?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/573009#M199718</link>
      <description>&lt;P&gt;Old post, but...&amp;nbsp; How about this?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;source=NameOfSource | streamstats count as row
|eval r=[search source=NameOfSource | streamstats count as row | search "Search Criterion" | head 1 | return $row]
|where row&amp;gt;=r-1 and row&amp;lt;=r+1 | sort -row&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 17:41:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/573009#M199718</guid>
      <dc:creator>scombs</dc:creator>
      <dc:date>2021-10-29T17:41:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to include the previous and/or following event after the search string appears?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/573016#M199721</link>
      <description>&lt;P&gt;Don't take it personally but that's one of the most ineffective solutions to any splunk problem I've seen. Don't ever do that.&lt;/P&gt;&lt;P&gt;Not only instead of using splunk's indexes you're scanning all events. You're doing it twice!&lt;/P&gt;&lt;P&gt;For reference -&lt;/P&gt;&lt;P&gt;simple search for the string "error" from my home splunk from _internal index for the last 24 hours&lt;/P&gt;&lt;P&gt;This search has completed and has returned &lt;SPAN class=""&gt;2,277&lt;/SPAN&gt; results by scanning &lt;SPAN class=""&gt;2,277&lt;/SPAN&gt; events in &lt;SPAN class=""&gt;1.846 &lt;/SPAN&gt;seconds&lt;/P&gt;&lt;P&gt;The "same" search by listing all events, streamstatsing and then searching:&lt;/P&gt;&lt;P&gt;This search has completed and has returned &lt;SPAN class=""&gt;2,279&lt;/SPAN&gt; results by scanning &lt;SPAN class=""&gt;1,155,856&lt;/SPAN&gt; events in &lt;SPAN class=""&gt;25.172 &lt;/SPAN&gt;seconds&lt;/P&gt;&lt;P&gt;I think you can see the difference.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 18:31:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/573016#M199721</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2021-10-29T18:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to include the previous and/or following event after the search string appears?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/573019#M199724</link>
      <description>&lt;P&gt;Hi PickleRick,&lt;/P&gt;&lt;P&gt;I greatly appreciate the feedback.&amp;nbsp; I've gained just enough experience that I want to start contributing back, so no offense taken.&lt;/P&gt;&lt;P&gt;By "source" I had in mind a specific, dated, application log which, in our environment, is quite constrained. Identifying (in our environment) just index and sourcetype would result in a quite broad result set.&lt;/P&gt;&lt;P&gt;I saw your solution, but took the poster's question as a desire for the couple of specific events immediately before and after the found event rather than a range of who-knows-how-many around its _time stamp.&lt;/P&gt;&lt;P&gt;If I had written my answer like the following, would it have been more acceptable?&lt;/P&gt;&lt;P&gt;index=YourIndex sourcetype=YourSourceType | streamstats count as row | eval r=[index=YourIndex sourcetype=YourSourceType | streamstats count as row | search "SpecificSearchCriterionExpectedToReturnOneEvent" | head 1 | return $row] | where row&amp;gt;=r-1 and r&amp;lt;=r+1 | sort -row&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 19:13:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/573019#M199724</guid>
      <dc:creator>scombs</dc:creator>
      <dc:date>2021-10-29T19:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to include the previous and/or following event after the search string appears?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/573024#M199727</link>
      <description>&lt;P&gt;Yes, I understand. And - to be honest - I also had similar need once or twice but - honestly - I don't think there's a "good" answer using splunk. It all boils down to the fact that there's no "sequence number" concept in splunk. That's why you either use time as your narrowing factor (which is not very precise as we know) or have to do such ineffective tricks as your solution.&lt;/P&gt;&lt;P&gt;The problem is that while with normal "index=whatever | search filter" splunk can optimize it and perform as if it was done simply as "index=whatever filter". But if you add streamstats, even if splunk could optimize the search just to find the proper result, it still has to look through all events just to count them.&lt;/P&gt;&lt;P&gt;So I suppose there's really no effective way to do that.&lt;/P&gt;&lt;P&gt;Also, there's no effective method not-involving a subsearch (because as you pass down the processing pipe you lose the knowledge about the original data, so you'd have to pass the data further downstream somehow which again means that you'd have to somehow keep a "backlog" of your events).&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 19:57:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-include-the-previous-and-or-following-event-after-the/m-p/573024#M199727</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2021-10-29T19:57:59Z</dc:date>
    </item>
  </channel>
</rss>

