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
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!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...