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

Fueling your curiosity with new Splunk ILT and eLearning courses

At Splunk Education, we’re driven by curiosity—both ours and yours! That’s why we’re committed to delivering ...

Splunk AI Assistant for SPL 1.1.0 | Now Personalized to Your Environment for Greater ...

Splunk AI Assistant for SPL has transformed how users interact with Splunk, making it easier than ever to ...

Unleash Unified Security and Observability with Splunk Cloud Platform

     Now Available on Microsoft AzureOn Demand Now Step boldly into the AI revolution with enhanced security ...