Splunk Search

Events with maximum value of dynamic field

Explorer

I think I'm missing something. I have rex generating a new field for me. I want to return only events with the maximum value for that field. Let's say the field is called testfield and the possible values for that field are 1, 2, or 3. I want to return only events with testfield=3. However, this will be in a dashboard and the next time it runs, the max value could be 4. How do I do this?

Tags (3)
1 Solution

SplunkTrust
SplunkTrust

Here's something that should work for you.

<your searchterms> | eventstats  max(testfield) as max | where testfield=max | fields - max

eventstats works much like its bigger brother stats, except that it paints its statistics back onto the original rows that were input, rather than aggregating/transforming the set. This generally means that it puts its stats back on the 'events' (although not necessarily - you can get good use out of eventstats further down the search pipeline sometimes too)

And if you're not actually interested in just the raw event text, but rather in one or more fields that are on those events, then a more straightforward way might be to do this:

<your searchterms> | stats max(someField) values(someOtherField) avg(someThirdField) by testField | sort - testField | head 1

View solution in original post

SplunkTrust
SplunkTrust

Here's something that should work for you.

<your searchterms> | eventstats  max(testfield) as max | where testfield=max | fields - max

eventstats works much like its bigger brother stats, except that it paints its statistics back onto the original rows that were input, rather than aggregating/transforming the set. This generally means that it puts its stats back on the 'events' (although not necessarily - you can get good use out of eventstats further down the search pipeline sometimes too)

And if you're not actually interested in just the raw event text, but rather in one or more fields that are on those events, then a more straightforward way might be to do this:

<your searchterms> | stats max(someField) values(someOtherField) avg(someThirdField) by testField | sort - testField | head 1

View solution in original post

Explorer

Perfect, that's exactly what I was trying to figure out how to do! Thanks!

0 Karma

Explorer

Great... 😄

0 Karma