Splunk Search

How to create a regex that removes everything after a second underscore?

gbwilson
Path Finder

I have a regex that should remove everything after a second underscore. When I try to search with the regex, it doesn't work. Any ideas? I must be doing something wrong, just can't figure out what.

Data looks like this:

AB200_Cdef_233
Abcde_FG400_34
And should end up looking like this:

AB200_Cdef
Abcde_FG400

    index=cms_vm
    | eval DatastoreName=replace(DatastoreName,"^[^_]*_[^_]*\K.*$")
    | table DatastoreName
    | dedup DatastoreName
    | sort DatastoreName
Tags (2)
0 Karma
1 Solution

FrankVl
Ultra Champion

Your regex is fine, you're just missing the mandatory 3rd argument of the replace(X,Y,Z) function.

It should work fine if you run it like this:

 index=cms_vm
 | eval DatastoreName=replace(DatastoreName,"^[^_]*_[^_]*\K.*$","")
 | table DatastoreName
 | dedup DatastoreName
 | sort DatastoreName

View solution in original post

FrankVl
Ultra Champion

Your regex is fine, you're just missing the mandatory 3rd argument of the replace(X,Y,Z) function.

It should work fine if you run it like this:

 index=cms_vm
 | eval DatastoreName=replace(DatastoreName,"^[^_]*_[^_]*\K.*$","")
 | table DatastoreName
 | dedup DatastoreName
 | sort DatastoreName

ddrillic
Ultra Champion

Maybe -

index=cms_vm
 | rex field=DatastoreName "(?<DatastoreName>.*_.*)_.*"
 | table DatastoreName
 | dedup DatastoreName
 | sort DatastoreName
0 Karma

rteja9
Path Finder

Try below query,

index=cms_vm
| eval DatastoreName=replace(DatastoreName,"^[^]+[^_]+(.*)","")
| table DatastoreName
| dedup DatastoreName
| sort DatastoreName

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...