Splunk Search

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


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

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


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!

Detecting Remote Code Executions With the Splunk Threat Research Team

WATCH NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If exploited, ...

Enter the Splunk Community Dashboard Challenge for Your Chance to Win!

The Splunk Community Dashboard Challenge is underway! This is your chance to showcase your skills in creating ...

.conf24 | Session Scheduler is Live!!

.conf24 is happening June 11 - 14 in Las Vegas, and we are thrilled to announce that the conference catalog ...