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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...