Splunk Search

How do I count the number of events based on the value of a field?

andrewtrobec
Motivator

Hello,

I am having trouble with a simple search. I have the following data:

OBJECT ID,NEW STATE
1,STATE ONE
1,STATE TWO
1,STATE THREE
2,STATE ONE
2,STATE TWO
2,STATE ONE
3,STATE ONE

...and so forth

I would like to return the number of events in which "NEW STATE" = "STATE ONE". I have the following search:

index = "SAMPLE INDEX" | stats count(eval("NEW STATE" = "STATE ONE")) as "COUNT"

Instead of returning "COUNT"=4, it returns 0 and I can't understand why. Is there something missing?

Any help would be greatly appreciated!

Thank you,

Andrew

0 Karma
1 Solution

niketn
Legend

As a optimized search you should include filter upfront rather than later. Following should work with better performance:

index = "SAMPLE INDEX" "NEW STATE"="STATE ONE"| stats count

If you intend to use your own query replace double quotes with single quotes for the 'New State' field. As within eval expression it does not match the field but value on both left and right hand side.

index = "SAMPLE INDEX" | stats count(eval('NEW STATE' = "STATE ONE")) as "COUNT"
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

View solution in original post

niketn
Legend

As a optimized search you should include filter upfront rather than later. Following should work with better performance:

index = "SAMPLE INDEX" "NEW STATE"="STATE ONE"| stats count

If you intend to use your own query replace double quotes with single quotes for the 'New State' field. As within eval expression it does not match the field but value on both left and right hand side.

index = "SAMPLE INDEX" | stats count(eval('NEW STATE' = "STATE ONE")) as "COUNT"
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

andrewtrobec
Motivator

Thanks for the suggestion and the solution to my problem!

0 Karma

MonkeyK
Builder

First off: you should consider changing your data definition to not include spaces in field names. I am not very experienced, but spaces in field names have already caused problems. Try denoting words by using CamelCase or separating them with underscores "_"

As to your question, if you are really only interested in a single state, you can filter your results before getting a count

index = "SAMPLE INDEX" | search "STATE ONE" | stats count

If you are trying to get counts for everything, you can just count by the field

index = "SAMPLE INDEX" | stats count by "NEW STATE"

But it is possible that Splunk will misinterpret the field "NEW STATE" because of the space in it, so it may just be found as "STATE". So if the above doesn't work, try this:

index = "SAMPLE INDEX" | stats count by "STATE"

sdaniels
Splunk Employee
Splunk Employee

You can use the Splunk top command. It will automatically give you a percentage as well and i've turned that off in the search below.

http://docs.splunk.com/Documentation/Splunk/6.5.0/SearchReference/Top

index = "SAMPLE INDEX" | top showperc=false "NEW STATE"

0 Karma
Get Updates on the Splunk Community!

Fueling your curiosity with new Splunk ILT and eLearning courses

At Splunk Education, we’re driven by curiosity—both ours and yours! That’s why we’re committed to delivering ...

Splunk AI Assistant for SPL 1.1.0 | Now Personalized to Your Environment for Greater ...

Splunk AI Assistant for SPL has transformed how users interact with Splunk, making it easier than ever to ...

Unleash Unified Security and Observability with Splunk Cloud Platform

     Now Available on Microsoft AzureOn Demand Now Step boldly into the AI revolution with enhanced security ...