Splunk Search

How to count all occurrences of certain strings from subsearch results?

lessard
Engager

I have text that is not well formatted, and I'm looking for occurrences of some text. In one spot, the text is easy to identify, so I'm identifying the text, and then finding all instances of that text in other areas of the log.

I currently have the following search working nicely to show me the lines I want to look at.

index=my_index [search index=my_index "[mysearch=" | rex "[mysearch=(?<mytext>[^\]]*)" | makemv delim="," mytext | rename mytext to query | fields query]

Given text like such:

[mysearch=111,222,333] Some random text and a row I want
[mysearch=444] Some random text and a row I want
[some silly line] this would not appear with the above query
[some other log]  Text that has 111 and 444 and a row I want but also numbers I do not want to count 1 2 3 555 
[some silly line] this would not appear with the above query 555 4 2 3 4 8 4444

My search pulls the correct three rows out. Now I'd like to count the occurrences of 111 and 222 and 333 and 444. For the above three lines giving me 2 counts for 111 and 444, and 1 count for 222 and 333, but I can't figure out how to get something like "stats count by mytext" to work.

Any suggestions?

Thanks.

Tags (3)
0 Karma

javiergn
Super Champion

Try this:

| your search here
| rex max_match=0 "=?(?<mytext>\d+)\,?"
| mvexpand mytext
| stats count by mytext

This is the output I'm getting based on the example given above:

mytext  count
111     2
222     1
333     1
444     2 

lessard
Engager

Thanks! Worked perfectly!
edit: I thought this was working, but the counts were far lower than I expected. I've modified the original question to show where I think some of the road bumps occur

0 Karma
Get Updates on the Splunk Community!

New in Observability - Improvements to Custom Metrics SLOs, Log Observer Connect & ...

The latest enhancements to the Splunk observability portfolio deliver improved SLO management accuracy, better ...

Improve Data Pipelines Using Splunk Data Management

  Register Now   This Tech Talk will explore the pipeline management offerings Edge Processor and Ingest ...

3-2-1 Go! How Fast Can You Debug Microservices with Observability Cloud?

Register Join this Tech Talk to learn how unique features like Service Centric Views, Tag Spotlight, and ...