Splunk Search

How to extract host field values with regex?

Raghav2384
Motivator

Hello Experts,

I am trying to extract hosts from the following in 2 ways
15 21:26:18 cmflouxy005.sample.xy.com storage_config_info[15580]: GETIT_Store_Reflection_data|INQ|cmflouxy005|/dev/rdsk/A3x5000097408315E90d63s2|000292603159|2606|R1|9437760
Approach # 1: i tried (^.*?\d{2}:d{2}:d{2})\s(?\S+)" this gives me cmflouxy005.sample.xy.com but i need 'cmflouxy005' only.

Approach # 2: Can you also help me how to extract the host value that's between the pipes on second line? by knowing both the ways, i am hoping to get a good understanding of regex and extractions involving special chars.

Another roadblock: sometimes i have IP addresses in place of FQDN in the logs.
Thanks in advance,
Raghav

0 Karma
1 Solution

musskopf
Builder

You could use like this, didn't test 100% but it should select the IP or the first part of the FQDN.

^\d+\s\d{2}:\d{2}:\d{2}\s(?<hostname>((\d+\.\d+\.\d+\.\d+|\w+)))

for the second line, with the Pipes, you can use:

^\w+\|\w+\|(?<hostname>(\d+\.\d+\.\d+\.\d+|\w+))

If you have the both lines on the same sourcetype, you probably want to give different names to the extracted fields.

Btw, this is a good site to test RegExps:
http://regex101.com/#pcre

View solution in original post

esix_splunk
Splunk Employee
Splunk Employee

For capturing IP, \d+ will work fine. This is probably a better example in terms of regex and not being greedy-

^\d+\s\d{2}:\d{2}:\d{2}\s(?<host>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|\w+)

And is that log actually two lines, or is it one single line event? In the later case, the second regex needs to be changed.. The below will look for the second occurrence of '|' from the beginning of the line and capture the word after that.

^(?:[^|]*\|){2}(?<hostname>\w[^|]*)

Cheers.

Raghav2384
Motivator

using d{1,3} instead of d+did the magic. Thank you

0 Karma

musskopf
Builder

You could use like this, didn't test 100% but it should select the IP or the first part of the FQDN.

^\d+\s\d{2}:\d{2}:\d{2}\s(?<hostname>((\d+\.\d+\.\d+\.\d+|\w+)))

for the second line, with the Pipes, you can use:

^\w+\|\w+\|(?<hostname>(\d+\.\d+\.\d+\.\d+|\w+))

If you have the both lines on the same sourcetype, you probably want to give different names to the extracted fields.

Btw, this is a good site to test RegExps:
http://regex101.com/#pcre

esix_splunk
Splunk Employee
Splunk Employee

d+ is a greedy search in regex. So it will return 0 to infinite numbers. Ip addresses do have constraints d{1,3}. So I would image the pattern matching in your data without a proper regex wouldnt identify it properly.

0 Karma

Raghav2384
Motivator

it worked after i changed \d+ to \d{1,3}. For some reason the field doesn't appear in interesting fields area if i use \d+. Do you know the reason?

Thank you for the link and appreciate you help.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...

Network to App: Observability Unlocked [May & June Series]

In today’s digital landscape, your environment is no longer confined to the data center. It spans complex ...

SPL2 Deep Dives, AppDynamics Integrations, SAML Made Simple and Much More on Splunk ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...