Splunk Search

split string with square brackets into individual fields

plcd63
Explorer

Hello,

I'm new to Splunk and I'm looking for some advice.

My search, e.g.

 

 

<mysearch> | table attributes

 

 

returns a value in the following format:

name[test 1]srcintf[int1]dstintf[int2]srcaddr[address1]dstaddr[dest1 dest2]service[svc1 svc2 svc3]comments[test comment here]

I would like to split the output into individual fields. The values are within square brackets, i.e.

nametest 1
srcintfint1
dstintfint2
... 

 

Many thanks!

Labels (2)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust
| rex "name\[(?<name>[^\]]*)]srcintf\[(?<srcintf>[^\]]*)]dstintf\[(?<dstintf>[^\]]*)]srcaddr\[(?<srcaddr>[^\]]*)]dstaddr\[(?<dstaddr>[^\]]*)]service\[(?<service>[^\]]*)]comments\[(?<comments>[^\]]*)]"

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust
| rex "name\[(?<name>[^\]]+)]srcintf\[(?<srcintf>[^\]]+)]dstintf\[(?<dstintf>[^\]]+)]srcaddr\[(?<srcaddr>[^\]]+)]dstaddr\[(?<dstaddr>[^\]]+)]service\[(?<service>[^\]]+)]comments\[(?<comments>[^\]]+)]"

plcd63
Explorer

Thank you! This works, unless one of the fields is empty, e.g. comments[], which is very likely to happen on some of them, depending on the user input provided.

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| rex "name\[(?<name>[^\]]*)]srcintf\[(?<srcintf>[^\]]*)]dstintf\[(?<dstintf>[^\]]*)]srcaddr\[(?<srcaddr>[^\]]*)]dstaddr\[(?<dstaddr>[^\]]*)]service\[(?<service>[^\]]*)]comments\[(?<comments>[^\]]*)]"

plcd63
Explorer

To take this step further, could I also use rex if not every time all of the values are part of the reported field?

e.g. my 1st search might return:

name[test 1]srcaddr[address1]

my second search:

nsrcintf[int1]dstintf[int2]srcaddr[address1]comments[test comment here]

my third search:

comments[test comment here]

etc...

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Yes, and sometimes you need to do this if your events don't have all the anchor strings - if this is the case, none of the fields are extracted if you use a single rex string i.e. there has to be a complete match for anything to be extracted. This is useful in some instances and not so much in others, but tbh, it is better this way around in the majority of cases.

Get Updates on the Splunk Community!

Your Guide to SPL2 at .conf24!

So, you’re headed to .conf24? You’re in for a good time. Las Vegas weather is just *chef’s kiss* beautiful in ...

Get ready to show some Splunk Certification swagger at .conf24!

Dive into the deep end of data by earning a Splunk Certification at .conf24. We're enticing you again this ...

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Now On-Demand Join us to learn more about how you can leverage Service Level Objectives (SLOs) and the new ...