Splunk Search

How to use Regex to trim the field?

kiran331
Builder

Hi, I have a field with DNS names, how to extract a host name from them?

for example,

abc123.ab.com
aca12.ba.xy.com

output i need

abc123
aca12

Tags (2)
0 Karma
1 Solution

DalJeanis
Legend

One way is

| rex field=mydnsfield "^(?<hostname>[^.]+)"

That will get everything that isn't a period, from the beginning, up to but not including the first period.

View solution in original post

0 Karma

DalJeanis
Legend

One way is

| rex field=mydnsfield "^(?<hostname>[^.]+)"

That will get everything that isn't a period, from the beginning, up to but not including the first period.

0 Karma

adonio
Ultra Champion

give this a try:
... | rex (?<field1>\w+)\.(?<field2>\w+)\.(?<field3>\w+)
https://regex101.com/r/uWt0Mf/1

hope it helps

DalJeanis
Legend

@adonio - (1) That's going to fail if any of the host names don't have three or more nodes. You should probably make the last one optional. (2) What's the purpose of the final .?

0 Karma

adonio
Ultra Champion

Thanks @DalJeanis!
wanted to demonstrate flexibility in case s/he needs more fields extracted
as always, great comments, appreciate it!
i guess the . implies i am writing more in my journal lately and i am trying to end a sentence with a .
edited the answer

0 Karma
Get Updates on the Splunk Community!

Detecting Remote Code Executions With the Splunk Threat Research Team

REGISTER NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If ...

Observability | Use Synthetic Monitoring for Website Metadata Verification

If you are on Splunk Observability Cloud, you may already have Synthetic Monitoringin your observability ...

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...