Splunk Search

How to extract a variable number of fields?

hulahoop
Splunk Employee
Splunk Employee

Given the following data sample of 4 events where each event has a number immediately after the timestamp that indicates the number of hosts to be listed:

10/07/2013:09:00:00 3 "host1" "host2" "host3" foo
10/07/2013:09:01:00 2 "host1" "host2" bar
10/07/2013:09:02:00 4 "host1" "host2" "host3" "host4" baz
10/07/2013:09:03:00 1 "host1" foobarbaz

So in the first event, there are 3 hosts (host1, host2 and host3) and an unrelated field following the 3 host names (foo).

Is it possible to use rex or props/transforms to intelligently capture the number of hosts expected, then use that number to capture the following host names into a multi-value field, then just pick up where we leave off and extract the fields following the host parade? I guess I could write a custom search command to return all the fields sensibly, but am thinking there might be a clever way to do this inline.

_d_
Splunk Employee
Splunk Employee

Yes. This produces a multivalued host field from the search bar:

| stats count| eval _raw="10/07/2013:09:02:00 4 host1 host2 host3 host4 foo bar vi is great" | rex "00\s(?<var>\d+)(?<rest>.*)"| rex field=rest max_match=10 "(?<rest>\S+?) " | eval host=mvindex(rest, 0, var-1)

Put the first rex as an EXTRACT in props.conf, the second as a REPORT with MV_ADD=10 in props & transforms.conf and the last eval in props as EVAL-host. Make sure you change the first rex regex so that it's anchored properly in your data.

Edit: Change max_match to a sensible number, too.

d.

kphillipson
Path Finder

Nice! I've learned something as well!

0 Karma

kphillipson
Path Finder

The first part is easy with regex however I can't think of a way to do the second part in splunk they way you would like.

You can do an extraction for each host in "" as the same field. If they are in "".

First part:
rex ":\d+\s(?P\d+)\s"
Second part:
rex "\"(?P[^"]+)\""

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...