Splunk Search

Need help on Regex for a field

tdavison76
Path Finder

Hello,  I am just trying to do a regex to split a single field into two new fields.

The original field is:

alert.alias = STORE_176_RSO_AP_176_10

I need to split this out to 2 new fields.

First field = STORE_176_RSO

Second field = AP_176_10

I am horrific at regex and am not sure how I can pull this off.  Any help would be awesome. 🙂 

Thank you for your help,

Tom

Labels (2)
0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

You can use the regex approach as @gcusello suggested, with a small modification:

 

| rex field=alert.alias "(?<field1>[^_]+(_[^_]+){2})_(?<field2>.+)"

 

Because the string is strictly formatted, you can also use split to achieve the same.  Depending on number of events you handle, the following could be more economical.

 

| eval elements = split('alert.alias', "_")
| eval field1 = mvjoin(mvindex(elements, 0, 2), "_"), field2 = mvjoin(mvindex(elements, 2, -1), "_")

 

Here is an emulation:

 

| makeresults format=csv data="alert.alias
STORE_8102_BOXONE_MX_8102
STORE_8102_BOXONE_MX_8102_01"

 

Either of the above searches gives

alert.aliasfield1field2
STORE_8102_BOXONE_MX_8102STORE_8102_BOXONEBOXONE_MX_8102
STORE_8102_BOXONE_MX_8102_01STORE_8102_BOXONEBOXONE_MX_8102_01

View solution in original post

gcusello
SplunkTrust
SplunkTrust

Hi @tdavison76 ,

if the structure of your field is always the same:

field1=chars_numbers_chars

separator=_

field2=chars_numbers_numbers

you can use a regex like the following:

| rex field=alert.alias "^(?<field1>\w+_\d+_\w+)_(?<field2>\w+_\d+_\d+)"

Ciao.

Giuseppe

tdavison76
Path Finder

Awesome, thank you very much, that did the trick.  I screwed up a little, after I tested it, I realized that I was wrong, the originating field can be like one of the following:

 

alert.alias = STORE_8102_BOXONE_MX_8102

alert.alias = STORE_8102_BOXONE_MX_8102_01

Is there a regex for the second field that would just capture everything after that third "_"?

Thanks again, really appreciate the help,

Tom

 

0 Karma

yuanliu
SplunkTrust
SplunkTrust

You can use the regex approach as @gcusello suggested, with a small modification:

 

| rex field=alert.alias "(?<field1>[^_]+(_[^_]+){2})_(?<field2>.+)"

 

Because the string is strictly formatted, you can also use split to achieve the same.  Depending on number of events you handle, the following could be more economical.

 

| eval elements = split('alert.alias', "_")
| eval field1 = mvjoin(mvindex(elements, 0, 2), "_"), field2 = mvjoin(mvindex(elements, 2, -1), "_")

 

Here is an emulation:

 

| makeresults format=csv data="alert.alias
STORE_8102_BOXONE_MX_8102
STORE_8102_BOXONE_MX_8102_01"

 

Either of the above searches gives

alert.aliasfield1field2
STORE_8102_BOXONE_MX_8102STORE_8102_BOXONEBOXONE_MX_8102
STORE_8102_BOXONE_MX_8102_01STORE_8102_BOXONEBOXONE_MX_8102_01

tdavison76
Path Finder

Hello,

Thank you very much for all of the details, that did the trick and I can finally move on to the next task.

Thanks again,

Tom

0 Karma
Get Updates on the Splunk Community!

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

Watch On Demand the Tech Talk on November 6 at 11AM PT, and empower your SOC to reach new heights! Duration: ...

Splunk Observability as Code: From Zero to Dashboard

For the details on what Self-Service Observability and Observability as Code is, we have some awesome content ...