Splunk Search

How to explicitly count an occurrence of a string when another similar string exists

WyldeRhoads
Engager

I am trying to count the occurrence of some specific strings in a field value. The below query works for counting occurences, but there are some strings that have similar names, and because of this the values can be inflated.

The results field is not formatted, and can contain the string BikeNew, BikeOld, and just Bike. The problem here is that searching for %Bike% will return counts for all 3, and therefor the new value "currentBikes" will count all 3 different bike variables. Is there a way to explicitly search for only Bike ?

index=foo source=bar
| eval newBikes = = if(like(resultsField,"%BikeNEW%"),1,0)
| eval currentBikes = = if(like(resultsField,"%Bike%"),1,0)
| eval oldBikes = = if(like(resultsField,"%BikeOLD%"),1,0)
| stats sum(currentBikes) AS currentBikeCount, sum(oldBikes) as oldBikeCount, sum(newBikes) as newBikeCount
0 Karma

yannK
Splunk Employee
Splunk Employee

As a first method, if your string "Bike" is surrounded by spaces, you can use
eval currentBikes = = if(like(resultsField,"% Bike %"),1,0)

but a proper method could be to use a regex instead of a like.

| rex field= resultsField "(?<bike_type>(Bike[^NO]|BikeNEW|BikeOLD)" 
| stats count by bike_type
0 Karma

micahkemp
Champion

rex "(?<plain_bike>Bike(?![ON]))"

That will create a field plain_bike when the word Bike is not followed by O or N.

Or, this will create a field you can use to count by bike:

rex "(?<bike>Bike(NEW|OLD)?)"

0 Karma
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...