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!

Accelerating Observability as Code with the Splunk AI Assistant

We’ve seen in previous posts what Observability as Code (OaC) is and how it’s now essential for managing ...

Integrating Splunk Search API and Quarto to Create Reproducible Investigation ...

 Splunk is More Than Just the Web Console For Digital Forensics and Incident Response (DFIR) practitioners, ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...