Splunk Search

Confused with sed replace. Change 1,2,...,20,21,22 to 1,2,...,20,###,22

ejpulsar
Path Finder

Hi,

I try to test sed expressions in search app for futher proper filtering of incoming data with SEDCMD.
Let 1 have a strings [0-9a-zA-Z]* delimited by commas. There can be no any string between commas.

i.e. 1,a,B,,D,5

I'm trying to build regex to change 21-th pattern occurence in the string to some fixed chars (#)

Splunk IFX gives me that perfect extraction regex

(?i)^(?:[^,]*,){21}(?P[^,]+)

I tried to transform it to sed replace

s/(?i)(?:[^,]*,){21}([^,]+,)/###,/

It gives me all string wiped from start to 22-th pattern occurence

 ###,22-th occurence,24,25

What am I doing wrong?

Tags (2)
0 Karma
1 Solution

martin_mueller
SplunkTrust
SplunkTrust

Sed works like this: Take what was matched by the first part out and replace by the second part... so all your 21 strings are being removed. You can avoid that like this:

s/(?i)((?:[^,]*,){21})([^,]+,)/\1###,/

That captures the matched strings and retains them before the ###. Here's a dummy query to test:

| stats count | eval string = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z" | rex field=string mode=sed "s/(?i)((?:[^,]*,){21})([^,]+,)/\1###,/"

View solution in original post

martin_mueller
SplunkTrust
SplunkTrust

Sed works like this: Take what was matched by the first part out and replace by the second part... so all your 21 strings are being removed. You can avoid that like this:

s/(?i)((?:[^,]*,){21})([^,]+,)/\1###,/

That captures the matched strings and retains them before the ###. Here's a dummy query to test:

| stats count | eval string = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z" | rex field=string mode=sed "s/(?i)((?:[^,]*,){21})([^,]+,)/\1###,/"

ejpulsar
Path Finder

You are awesome right!
Thank you!

0 Karma
Get Updates on the Splunk Community!

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

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