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!

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...