Splunk Search

How to replace string using rex with partial matched string?

LearningGuy
Motivator

How to replace string using rex with partial matched string?
Thank you for your help.
For example:
I tried to replace "::" (double colon) with ":0:"  (colon zero colon) if it the previous characters contain ":" followed by "1 to 3 characters" 
| rex mode=sed field=ip "s/:.{1,3}::/:.{1,3}:0:/g"   => this does not work because it will literally replace it with
":.{1,3}:0:"   instead of retaining the matched strings

Before
a0:1::21
b0:1c::21
c0:a13::23

After
a0:1:0:21
b0:11:0:21
c0:111:0:23

Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Characters to be retained should be enclosed in a capture group and that group referenced in the replacement text.

| rex mode=sed field=ip "s/:(.{1,3})::/:\1:0:/g" 
---
If this reply helps you, Karma would be appreciated.

View solution in original post

LearningGuy
Motivator

It worked!!  Thank you..    

richgalloway
SplunkTrust
SplunkTrust

Characters to be retained should be enclosed in a capture group and that group referenced in the replacement text.

| rex mode=sed field=ip "s/:(.{1,3})::/:\1:0:/g" 
---
If this reply helps you, Karma would be appreciated.
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...