Splunk Search

Get the 10 recent events for each host.

allen_hunter
Explorer

I am trying to write a search that will pull the 10 (or so) most recent events for each host. The tail and head commands apparently do not allow any grouping, and I am trying to wrap my head around how to do this.

I know this does not work, but this is what I am looking for:

index=index1
| head 10 by host

The closest I can come up with is:

 index=index1
| stats values(_raw) by host

But that still gives me everything in the time range, not just the last 10 events per host.

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

There are couple of other ways to do that.  I like the dedup command because it's simple.  It retains the most recent n events by the specified field(s).

index = index1
| dedup 10 host

 This method is for those who dislike dedup.  It counts events by host and then takes those with a count <=10.

index = index1
| streamstats count by host
| where count <= 10
| fields - count
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

There are couple of other ways to do that.  I like the dedup command because it's simple.  It retains the most recent n events by the specified field(s).

index = index1
| dedup 10 host

 This method is for those who dislike dedup.  It counts events by host and then takes those with a count <=10.

index = index1
| streamstats count by host
| where count <= 10
| fields - count
---
If this reply helps you, Karma would be appreciated.

allen_hunter
Explorer

I did not know you could add a count to the dedup statement. This is good info to know. 

I completely forgot about trying the streamstats method. But I have used it in the past. Will put it in my notes for future reference.

Thanks!

allen_hunter
Explorer

This seems to work, but feels a little "hack-ish":

 

index=txdir_mainframe 
| transaction host maxevents=20
| dedup host

If anyone has any better ideas, I am open to suggestions.

Thanks, 

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...