Splunk Search

Will you help me enumerate some duplicate records returned in a search into another field?

chris94089
Path Finder

Greetings!

I have duplicate data. But that's ok. I actually don't want to just remove my dupes, I want to create another field that will enumerate them.

start with something like this:

myfield | _time
+++++++++++++
record1 | _time1
record1 | _time2
record1 | _time3
record2 | _time4

and turn into this (using eval level=?):

myfield | _time | level
+++++++++++++++++++++++++
record1 | _time1 | 1
record1 | _time2 | 2
record1 | _time3 | 3
record2 | _time4 | 1

I feel like Splunk could do something like this with tools like sortby and split by. Any thoughts? Thanks!

0 Karma
1 Solution

renjith_nair
Legend

@chris94089 ,

Try

"your search to get myfield and _time"|streamstats count as level reset_on_change=true by myfield 
---
What goes around comes around. If it helps, hit it with Karma 🙂

View solution in original post

0 Karma

renjith_nair
Legend

@chris94089 ,

Try

"your search to get myfield and _time"|streamstats count as level reset_on_change=true by myfield 
---
What goes around comes around. If it helps, hit it with Karma 🙂
0 Karma

chris94089
Path Finder

Hey, renjith.nair.

Thanks for the reply. Will this work if the data is not in real time? It's structured JSON. Perhaps _time should be just date_time. sorry 😕

0 Karma

macadminrohit
Contributor

Can you provide some sample data ?

0 Karma

macadminrohit
Contributor

You can try something like this

 `| makeresults 
| eval data="Record1,_field1:Record1,_field2:Record3,_field3:Record4,_field4" 
| makemv delim=":" data 
| mvexpand data 
| rename data as _raw 
| kv 
| fields - _time 
| rename _raw as data 
| mvexpand data 
| makemv delim="," data 
| eval Records=mvindex(data,0) 
| eval Fields=mvindex(data,1) 
| fields - data 
| streamstats count by Records reset_on_change=true`
0 Karma

renjith_nair
Legend

yes it will work if the data is not in real time as well and does not depend on time field. So as long as long you have the field myfield it should work.

Here is a run anywhere example

| makeresults  count=10|eval myfield="abc"| streamstats count|eval myfield=if(count%3==0,"xyz","abc")|fields - count
|streamstats count as level reset_on_change=true  by myfield

In the above case, if you want a continuous count for let's say abc instead of starting from 1 in the next occurance, just remove reset_on_change=true

---
What goes around comes around. If it helps, hit it with Karma 🙂
0 Karma

chris94089
Path Finder

This looks to be working for my situation, thank you so much!

Since I have lots of other fields, I needed to add a sort on the relevant fields before Splunk could start numbering things correctly. Very interesting behavior.

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...