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
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

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