Splunk Search

How to edit my regular expression to extract the last IP from my sample string using rex?

bakalon
Explorer

Hey Guys,

I have the following output:

Server: abc-ij-qwerty88.asdf.xyz.com Address: 10.10.254.97 DNS request timed out. timeout was 2 seconds. DNS request timed out. timeout was 2 seconds. Name: google.com Address: 172.217.20.46

I'd like to extract the Last IP from the string.

I tried the following search with rex, but it returns a blank result:

.... | rex field=_raw "(?\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)" | table  ip_address

Where did I go wrong?

Your help is appreciated. Thanks!

0 Karma
1 Solution

sundareshr
Legend

There's probably a LF at the end. Try this

... | rex "(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\n\t\s]$" | ...

View solution in original post

inventsekar
SplunkTrust
SplunkTrust

just move the "$" outside the rex match.

  1. Rex match should be inside parentheses "(.....)",
  2. and to position the rex match, we can use this anchor "$", (which means at the end of line, ...opposite of "^" - beginning of the line)
 .... | rex field=_raw "(?\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$" | table  ip_address
thanks and best regards,
Sekar

PS - If this or any post helped you in any way, pls consider upvoting, thanks for reading !
0 Karma

bakalon
Explorer

Thanks for your help! This didn't work, but the inclusion of [\n\t\s] at the end (1st answer) worked for me. Thanks again for your help.

0 Karma

sundareshr
Legend

There's probably a LF at the end. Try this

... | rex "(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\n\t\s]$" | ...

bakalon
Explorer

Thank you! This worked for me. What did you mean by an "LF" at the end?

0 Karma

sundareshr
Legend

Line feed. or line break

0 Karma

gokadroid
Motivator

you did not put the name of the tag ?<ip_address> which you are tabl(ing)

 .... | rex field=_raw "(?<ip_address>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$" | table  ip_address
0 Karma

bakalon
Explorer

Sorry, yeah I did use the tag but somehow forgot it when making this post. I tried your query but it produced the same blank results. The first answer solved my issue. Thanks!

0 Karma

somesoni2
Revered Legend

You were almost there. Use like this

.... | rex field=_raw "(?<ip_address>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$" | table  ip_address
0 Karma

bakalon
Explorer

I had tried this earlier, but it didn't work for me. Thanks for taking the time to help me!

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...