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!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...