Splunk Search

aggregating field values

jedatt01
Builder

I have a specific field that has similar values that I want to group together and obtain an average of another fields value. For example, if the field had the values Bob Sanders, Bob Baker, Sam Winters, and Sam Smith. Each one of these has a numeric value in another field. I would want to group the Bob's and Sam's together and get the average of the values of the numeric field for each.

Like this
Bob, 20
Sam, 36

Anybody have idea's?

Tags (3)
1 Solution

araitz
Splunk Employee
Splunk Employee

Assuming that the CSV looks like this:

first,last,age
Bob,Sanders,23
Sam,Johnson,36
Bob,Jackson,39
Sam,Conrad,21

The search would look like:

first=Bob OR first=Sam | stats avg(age) by first

The results would look like this:

first     avg(age)
------------------
Bob       31
Sam       28.5

View solution in original post

0 Karma

amstaff
Explorer

What you have is a multivalue field. To get to the values use the mvindex command.
mvindex(multifield, 2)

First check if a field is multivalued:
mvcount(multifield)

To get to the values In your case:
| eval name=mvindex(yourmultifield,0) | eval surname=mvindex(yourmultifield,1) | ...

You can also split a field to get a multivalued field.
split(foo, ";")

http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/CommonEvalFunctions

0 Karma

araitz
Splunk Employee
Splunk Employee

Assuming that the CSV looks like this:

first,last,age
Bob,Sanders,23
Sam,Johnson,36
Bob,Jackson,39
Sam,Conrad,21

The search would look like:

first=Bob OR first=Sam | stats avg(age) by first

The results would look like this:

first     avg(age)
------------------
Bob       31
Sam       28.5
0 Karma

araitz
Splunk Employee
Splunk Employee

The best approach would be to use a field extraction on the field. Conf files are best (using SOURCE_KEY in transforms.conf), but you can also try it in the search language. Let's assume that there is a name field that looks like "Bob Johnson":

`name="Bob *" OR name="Sam *" | rex field=name "(?<first_name>\S+)" | stat avg(age) by first_name`
0 Karma

jedatt01
Builder

hey araitz, this is jdattilo. It's actually a little more complicated than I first explained. I'm using fictitious data here, but lets say that the data in first and last fields are actually one single field. Can I do a kind of field 'contains' or 'starts with' type of thing? I tried to use substring, which kind of worked but not all my fields entries are the same length so some letters got cut off.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Fuel Your Journey: What’s Waiting for You at the .conf26 Acceleration Station

Navigating the show floor at .conf26 isn't just about keynotes and technical breakout sessions; it's also ...

Join the Final Session of the Data Management & Federation Bootcamp Series

Over the past three sessions of the Data Management & Federation Bootcamp Series, we've explored how to build ...

From Data to Insight: Announcing the Winners of the Splunk Dashboard Contest

Hi Splunkers, First off, thank you to everyone who participated in our very first From Data to Insight: The ...