Splunk Search

Remove leading zeros from IP address using regex

Splunk_sid
Loves-to-Learn Everything

Hello All,

I'm trying to remove leading zeros in IP addresses using rex and mode=sed . the regular expression I'm trying to use for substitution is "\b0+\B" . However, it's not returning the required output.

Example : | rex field=<IP address field> mode=sed "\b0+\B"

I even tried with double back slash. But, no luck. Kindly assist to resolve this issue.

Regards,

Sid

Labels (1)
0 Karma

glc_slash_it
Path Finder

Hi,
try this sed expression instead

...
| rex field=ip mode=sed "s/\b0+([0-9]+)/\1/g"

 

------------
If this was helpful, some karma would be appreciated.

0 Karma

bowesmana
SplunkTrust
SplunkTrust

That's not a valid rex sed statement, use this example

| makeresults 
| eval ip=split("010.1.2.3,10.013.2.3",",")
| mvexpand ip
| rex field=ip mode=sed "s/\b0+\B//"

 

0 Karma

Splunk_sid
Loves-to-Learn Everything

@bowesmana  I'm still not getting the expected result. The regex pattern should remove all leading zeros in each octet. however, in this case, it is just removing the leading zero in whichever octet it finds at first place. 

For example:

1) 010.1.2.3 -> it removes zero in 1st octet - > 10.1.2.3

2) 10.001.2.3 -> it removes zero in 2nd octet -> 10.1.2.3

3) 010.001.2.3 -> it removes zero in 1st octet only -> 10.001.2.3

4) 10.001.002.3 -> it removes zero in 2nd octet only -> 10.1.002.3

5) 10.1.2.003 -> it removes zero in 2nd octet only -> 10.1.2.3

 

Regards,

Sid

 

0 Karma

bowesmana
SplunkTrust
SplunkTrust

My bad, it was missing the 'g' (global) flag at the end

| makeresults 
| eval ip=split("010.1.2.3,10.013.2.3,010.001.002.003",",")
| mvexpand ip
| rex field=ip mode=sed "s/\b0+\B//g"

 

0 Karma
Get Updates on the Splunk Community!

Index This | What goes away as soon as you talk about it?

May 2025 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with this month’s ...

What's New in Splunk Observability Cloud and Splunk AppDynamics - May 2025

This month, we’re delivering several new innovations in Splunk Observability Cloud and Splunk AppDynamics ...

Getting Started with Splunk Artificial Intelligence, Insights for Nonprofits, and ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...