Splunk Search

Using eval to Add Field for top Result?

splunkyphil
Engager

I need to create a new field to assign to the top results of a command using eval

Obviously this syntax doesn't work, so I'm looking for the correct query:

source="tutorialdata.zip*" | eval popular = top limit=5 itemId | stats count(action) by popular, action

Basically I only need the action stats of the top 5 itemId results of the following:

Screen Shot 2022-10-23 at 7.43.46 PM.png

 

Sorry for the n00b question; I am just getting started with Splunk. Thanks for your time!

Labels (1)
0 Karma
1 Solution

bowesmana
SplunkTrust
SplunkTrust

Is this what you're after?

 

source="tutorialdata.zip*"
| eval X_{action}=action
| stats count as total count(X_*) as X_* by itemId
| sort - total
| head 5
| rename X_* as *
| fields - total

 

That will give you 5 rows of the most frequent itemId and then each column is a count of actions

Note the X_ prefix to the new field created is to allow the use of wildcards in the stats command to only collect the fields you're interested in.

You could also replace the eval/stats with 

| eval X_{action}=1
| stats count as total sum(X_*) as X_* by itemId

which may be slightly more efficient

View solution in original post

bowesmana
SplunkTrust
SplunkTrust

Is this what you're after?

 

source="tutorialdata.zip*"
| eval X_{action}=action
| stats count as total count(X_*) as X_* by itemId
| sort - total
| head 5
| rename X_* as *
| fields - total

 

That will give you 5 rows of the most frequent itemId and then each column is a count of actions

Note the X_ prefix to the new field created is to allow the use of wildcards in the stats command to only collect the fields you're interested in.

You could also replace the eval/stats with 

| eval X_{action}=1
| stats count as total sum(X_*) as X_* by itemId

which may be slightly more efficient

splunkyphil
Engager

This works; thank you kindly!

0 Karma
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...