Splunk Search

How to count a field by another one and removes duplicates?

ERFFFFF
Explorer

Hello everyone !

After a few hours of research i come ask your help.

Here is my data :

Username_column clientip_column
username1 xxx.xxx.xxx.xxx
username1 xxx.xxx.xxx.xxx
username1 xxx.xxx.xxx.xxx
username1 yyy.yyy.yyy.yyy
username2 xxx.xxx.xxx.xxx
username2 zzz.zzz.zzz.zzz
username3 yyy.yyy.yyy.yyy
username3 xxx.xxx.xxx.xxx

 

So, what i would like to do is to create another column called "countUsername" which contain the number of usernames by clientip without duplicates (of usernames).

Here is my dream table (what i want) :

Username_column clientip_column countUsername
username1 xxx.xxx.xxx.xxx 3
username1 xxx.xxx.xxx.xxx 3
username1 xxx.xxx.xxx.xxx 3
username1 yyy.yyy.yyy.yyy 2
username2 xxx.xxx.xxx.xxx 3
username2 zzz.zzz.zzz.zzz 1
username3 yyy.yyy.yyy.yyy 2
username3 xxx.xxx.xxx.xxx 3

 

I tried various of things like :

| eventstats values(count(Username_column)) as countUsername by clientip_column

 creating a multivalue column and trying of mvdedup().

combine my Username_column and my clientip_column like so :

| eval countUsername=Username_column. " " . clientip_column

 and doing lots of things on that, if(mach)), regex, ...

But everything that i tried didn't work. The best thing that i can get is :

| eventstats count(Username_column) as countUsername by clientip_column

 But with this line, my usernames are duplicated. (like the table bellow, i tried some things with this result but no results on my side)

Username_column clientip_column countUsername
username1 xxx.xxx.xxx.xxx 5
username1 xxx.xxx.xxx.xxx 5
username1 xxx.xxx.xxx.xxx 5
username1 yyy.yyy.yyy.yyy 2
username2 xxx.xxx.xxx.xxx 5
username2 zzz.zzz.zzz.zzz 1
username3 yyy.yyy.yyy.yyy 2
username3 xxx.xxx.xxx.xxx 5

 

Maybe you are wondering why i'm using eventstats instead of stats. The reason is that before this line, i have a large search with multiple stats commands, and if i don't use eventstats, all my others columns at the end of my large request won't show up.

Kind regards,

Labels (4)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

You were so very close.  Use the distinct_count function to count unique values of a field.

| eventstats dc(Username_column) as countUsername by clientip_column

 

---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

You were so very close.  Use the distinct_count function to count unique values of a field.

| eventstats dc(Username_column) as countUsername by clientip_column

 

---
If this reply helps you, Karma would be appreciated.

ERFFFFF
Explorer

Damm.... indeed close...
Thank you very much for your help !

0 Karma

somesoni2
Revered Legend

Just add this to your current search

| eventstats dc(Username_column) as countUsername by clientip_column
Get Updates on the Splunk Community!

Troubleshooting the OpenTelemetry Collector

  In this tech talk, you’ll learn how to troubleshoot the OpenTelemetry collector - from checking the ...

Adoption of Infrastructure Monitoring at Splunk

  Splunk's Growth Engineering team showcases one of their first Splunk product adoption-Splunk Infrastructure ...

Modern way of developing distributed application using OTel

Recently, I had the opportunity to work on a complex microservice using Spring boot and Quarkus to develop a ...