Splunk Search

Count number of times a field appears in a single event?

the_wolverine
Champion

I have an event which contains many instances of the same field. Additionally the field is multi-valued. How can I count the instances of this field within an event (by value)?

Example (this is a single event):

"Line" = (
  {
    "supplier" = "ABCDEFG";
    "errorCode" = "0";
    "otherItem" = "GFY";
    "quantity" = "1";
    "Number" = "sfldksfjkdsfjklsdfklsfjskdlfjdklfjklsdfjkds";
    "magicID" = "thisisthesame";
    };
  },
  {
    "supplier" = "WZBFK";
    "errorCode" = "0";
    "otherItem" = "GFY";
    "quantity" = "1";
    "Number" = "slgkfhortirtiroetiroeptieprtoeritorpetreo";
    "magicID" = "thisisthesame";
    };
  },
  {
    "supplier" = "TYIIRE";
    "errorCode" = "0";
    "otherItem" = "GFY";
    "quantity" = "3";
    "Number" = "mnssdoerptimgmfdgdfgdfeo";
    "magicID" = "thisisthesame";
    };
  },
0 Karma

sideview
SplunkTrust
SplunkTrust

This doesn't seem to be a fabulous answer, but one way is to actually split the eventtext into a multivalue field, mvexpand on that field, while using streamstats to keep track of which of the expanded rows came from which of the original events.

Because there are newlines you might have to use some "\n" here, I'm not sure. This seems to work here on your raw text doing some quick tests but ymmv.

| streamstats count as eventIndex | eval mvRaw=split(_raw,"},

{") | mvexpand foo

From there, the drawback is you'd have to re-extract your field values with rex or json commands as appropriate, but once you did that you could do stats dc(supplier) dc(errorCode) by eventIndex

0 Karma

martin_mueller
SplunkTrust
SplunkTrust

Got an example?

0 Karma
Get Updates on the Splunk Community!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...