Splunk Search

How do I get mvcount to display 0 when no field exists?

DamianS
Explorer

Hi all,

I have a search that looks something like this:

foo | extract pairdelim="|;]}" kvdelim="=:" mv_add=true | eval legs=mvcount(Leg)+1 | stats count by id, legs

The multi value field "Leg" has one of the 3 possible values "", "2", "2 3". I expected mvcount to work like count and when there are no values to display 0. Is there a way to display what I would I expect? Explicitly the mapping I wanted to do was from the multi value field "Leg": No Value -> 1, "2" -> 2, "2 3" -> 3

Any idea?

Tags (2)
0 Karma

cphair
Builder

The documentation says mvcount returns the number of values of the field if it's multivalued, 1 if it's singlevalued, and NULL otherwise. Given the mapping you describe, I think you want to do something like this:


... | eval legs=if(isnull(Leg),1,mvcount(Leg)+1)

cphair
Builder

You're welcome, @DamianS. Would you mind clicking the checkmark next to my answer to accept it? Thanks.

0 Karma

DamianS
Explorer

Perfect! Thanks for your help!

0 Karma
Get Updates on the Splunk Community!

Shape the Future of Splunk: Join the Product Research Lab!

Join the Splunk Product Research Lab and connect with us in the Slack channel #product-research-lab to get ...

Auto-Injector for Everything Else: Making OpenTelemetry Truly Universal

You might have seen Splunk’s recent announcement about donating the OpenTelemetry Injector to the ...

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...