Splunk Search

How to extract an unknown number of fields with rex command?

jbrenner
Path Finder

Let's say I have data in an event that looks like this:

 

 

 

NAME: John
NAME: Mary 
NAME: Sue

 

 

 

Assuming I have no idea how many names will exist in the event, is it possible to use the rex command to parse out all the names and display them in separate fields?

Thanks,

Jonathan

Labels (1)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

You can use the rex command with the max_match option to match an unpredictable number of names.

| rex max_match=0 "NAME: (?<Name>\S+)"

It will, however, put all of the matches into the single (multi-value) field called "Name".  You then can use the mvexpand command to put each Name value into a separate event.  That's not exactly "separate fields", though, is it?

What exactly do you mean by "separate fields"?  What should the final result look like?

---
If this reply helps you, Karma would be appreciated.
0 Karma

yuanliu
SplunkTrust
SplunkTrust

I suspect that the OP meant separate events rather than separate fields, like

| rex max_match=0 "NAME: (?<Name>\S+)"
| mvexpand Name

But to humor the literal interpretation, you could do something like

| rex max_match=0 "NAME: (?<Name>\S+)"
| foreach 1 2 3 4 5 6 7 8 9 10 ``` more than mvcount(Name) ```
  [eval "Name-<<FIELD>>" = mvindex(Name, <<FIELD>> - 1)]

This uses a side effect of SPL's handling of null assignments. (I wish the new multivalue foreach's <<ITEM>> could be used on the left-hand side.  It cannot.)

0 Karma

bowesmana
SplunkTrust
SplunkTrust

Another "technical" solution for making new fields is this, which first makes the name of the field, the name from the event, then uses streamstats to define the Name suffix, so doesn't have a foreach limitation.

| makeresults
| eval _raw="NAME: John
NAME: Mary 
NAME: Sue
NAME: Jack
NAME: Jill
NAME: Peter
NAME: Susan"
| rex max_match=0 "NAME: (?<Name>\S+)"
| mvexpand Name
| streamstats c
| eval x_{Name}=c
| stats values(x_*) as *
| foreach * [ eval "Name_{<<FIELD>>}"="<<FIELD>>" ]
| fields Name_*

 I say "technical", as this type of solution is rarely a real-life solution where mvexpand is an option in a large data set. 

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

Thanks for the Memories! Splunk University, .conf25, and our Community

Thank you to everyone in the Splunk Community who joined us for .conf25, which kicked off with our iconic ...

Data Persistence in the OpenTelemetry Collector

This blog post is part of an ongoing series on OpenTelemetry. What happens if the OpenTelemetry collector ...

Introducing Splunk 10.0: Smarter, Faster, and More Powerful Than Ever

Now On Demand Whether you're managing complex deployments or looking to future-proof your data ...