Splunk Search

How to use rex to remove the domain from the "User name" field and use the username only as a named extraction?

kris99
New Member

How do I use regex within search to remove the domain from the field "User name" and use the username only as named extraction.

domain\username

something like this i think but don't know who to write regex to extract username or extract everything after "\" from field "User name"

| rex field="User name" "" | eval UserName=lower(UserName) | where UserName=lower(UserName) | search UserName="*"

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Try this:

... | rex field="User name" "(?<domain>\S+)\\\\(?<userName>\S+)" | eval userName=lower(userName) | ...

If Splunk doesn't like a field name with a space in it, try this:

... | eval domainUsername="User name" | rex field=domainUsername "(?<domain>\S+)\\\\(?<userName>\S+)" | eval userName=lower(userName) | ...
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

Try this:

... | rex field="User name" "(?<domain>\S+)\\\\(?<userName>\S+)" | eval userName=lower(userName) | ...

If Splunk doesn't like a field name with a space in it, try this:

... | eval domainUsername="User name" | rex field=domainUsername "(?<domain>\S+)\\\\(?<userName>\S+)" | eval userName=lower(userName) | ...
---
If this reply helps you, Karma would be appreciated.

richgalloway
SplunkTrust
SplunkTrust

Please accept the answer.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

yes i did.

just so i learn how to write regex, if it was seperated by : what would i replace it with ?

0 Karma

richgalloway
SplunkTrust
SplunkTrust

In the regex in the answer, the four backslashes are the separator between the domain and username. If the separator becomes ':' then the regex becomes "(?\S+):(?\S+)".

A good way to learn is through experimentation. Try regexr.

---
If this reply helps you, Karma would be appreciated.
0 Karma

richgalloway
SplunkTrust
SplunkTrust

The escape character needs to be escaped. I've updated the answer.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

works like a charm.. thank you !

0 Karma

richgalloway
SplunkTrust
SplunkTrust

What do you get?

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

same error above

0 Karma

richgalloway
SplunkTrust
SplunkTrust

The backslash needs to be escaped.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

only using this.. no luck

rex field=domainUsername "(?<domain>\S+)\\(?<userName>\S+)" 
0 Karma

richgalloway
SplunkTrust
SplunkTrust

The parts between < and > define a Splunk field into which rex will extract matches. They're not placeholders. Change "domain-22" back to "domain" and it should work.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

still getting same error. tried both options above

Error in 'rex' command: Encountered the following error while compiling the regex '(?<domain>\S+)\(?<userName>\S+)': Regex: unmatched parentheses 
0 Karma

kris99
New Member

getting an error as below. domain includes domain-22\username

Error in 'rex' command: Encountered the following error while compiling the regex '(?<domain-22>\S+)\(?<userName>\S+)': Regex: unmatched parentheses 
0 Karma

richgalloway
SplunkTrust
SplunkTrust

Just what? If there's a character between the quotation marks, it's not showing up. Escape the character or use backtics.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

editor is removing backward slash

0 Karma

kris99
New Member
 domain\username
0 Karma

richgalloway
SplunkTrust
SplunkTrust

What separates domain from username? Please share a sample of your data.

---
If this reply helps you, Karma would be appreciated.
0 Karma

kris99
New Member

just "\"

"User name"=domain\username

0 Karma
Get Updates on the Splunk Community!

Troubleshooting the OpenTelemetry Collector

  In this tech talk, you’ll learn how to troubleshoot the OpenTelemetry collector - from checking the ...

Adoption of Infrastructure Monitoring at Splunk

  Splunk's Growth Engineering team showcases one of their first Splunk product adoption-Splunk Infrastructure ...

Modern way of developing distributed application using OTel

Recently, I had the opportunity to work on a complex microservice using Spring boot and Quarkus to develop a ...