Splunk Search

How to trim everything from a field after a comma

Engager

I have a field that contains:

CN=Joe Smith,OU=Support,OU=Users,OU=CCA,OU=DTC,OU=ENT,DC=ent,DC=abc,DC=store,DC=corp

I'd like to trim off everything after the first comma.

This information can always be changing, so there is no set number of characters.

Thanks.

0 Karma
1 Solution

Ultra Champion

Hi @sawyer2624
try

|rex field=yourfield "CN\=(?P<commonName>[^\,]+)"

View solution in original post

Builder

Using sed mode on rex is one way to do it:

| makeresults
| eval thefield="CN=Joe Smith,OU=Support,OU=Users,OU=CCA,OU=DTC,OU=ENT,DC=ent,DC=abc,DC=store,DC=corp"
| rex field=thefield mode=sed "s/([^,]+).*/\1/g"

This Run Anywhere example shows a replace in-situ, which has the side effect of forever altering "thefield" going forward.

See https://regex101.com/r/EHmicm/1

Ultra Champion

Hi @sawyer2624
try

|rex field=yourfield "CN\=(?P<commonName>[^\,]+)"

View solution in original post

Engager

So that creates a new field just like I want it, but how do I get the original field out of the table?

0 Karma

Ultra Champion
|rex field=yourfield "CN\=(?P<commonName>[^\,]+)"|fields - yourfield
0 Karma
State of Splunk Careers

Access the Splunk Careers Report to see real data that shows how Splunk mastery increases your value and job satisfaction.

Find out what your skills are worth!