Splunk Search

Have to remove the last section of IP addresses before getting a stats count for each one?

ewanbrown
Path Finder

Hi,

I have a list of IPs, and I want to create a chart showing traffic from them, but I also want a version which excludes the last section. As IPs can be varying lengths, I'm not sure how to do this. I think I need to find the 3rd, and then remove the data after that.

e.g. if I had 3 events

1.2.3.4
1.2.3.5
123.456.567.345

I'd want to show a table like this:

1.2.3 = 2
123.456.567 = 1

Thanks

0 Karma
1 Solution

dshpritz
SplunkTrust
SplunkTrust

You can do this pretty easily by adding a new field extraction. An example would be:

In props.conf:

[mysourcetype]
EXTRACT-ip_start = ^(?<ip_start>\d{1,3}\.\d{1,3}\.\d{1,3})\.\d{1,3} IN myfield

In this case, mysourcetype would be the name of the sourcetype you need this field added to, and then "myfield" would be the name of the existing field that contains the IP address. Note that you may need to adjust the extraction to make sure that the field you are extracting this from exists prior to the extraction above (see here for parsing order at search time).

Alternatively, you can use the rex command and do something like this:

sourcetype=mysourcetype | rex field=myfield ^(?<ip_start>\d{1,3}\.\d{1,3}\.\d{1,3})\.\d{1,3} | stats count by ip_start

HTH,

Dave

Edit: Added a more exact regex. Thanks IRC!

View solution in original post

dshpritz
SplunkTrust
SplunkTrust

You can do this pretty easily by adding a new field extraction. An example would be:

In props.conf:

[mysourcetype]
EXTRACT-ip_start = ^(?<ip_start>\d{1,3}\.\d{1,3}\.\d{1,3})\.\d{1,3} IN myfield

In this case, mysourcetype would be the name of the sourcetype you need this field added to, and then "myfield" would be the name of the existing field that contains the IP address. Note that you may need to adjust the extraction to make sure that the field you are extracting this from exists prior to the extraction above (see here for parsing order at search time).

Alternatively, you can use the rex command and do something like this:

sourcetype=mysourcetype | rex field=myfield ^(?<ip_start>\d{1,3}\.\d{1,3}\.\d{1,3})\.\d{1,3} | stats count by ip_start

HTH,

Dave

Edit: Added a more exact regex. Thanks IRC!

ewanbrown
Path Finder

Thanks, that's perfect

0 Karma
Get Updates on the Splunk Community!

.conf25 technical session recap of Observability for Gen AI: Monitoring LLM ...

If you’re unfamiliar, .conf is Splunk’s premier event where the Splunk community, customers, partners, and ...

A Season of Skills: New Splunk Courses to Light Up Your Learning Journey

There’s something special about this time of year—maybe it’s the glow of the holidays, maybe it’s the ...

Announcing the Migration of the Splunk Add-on for Microsoft Azure Inputs to ...

Announcing the Migration of the Splunk Add-on for Microsoft Azure Inputs to Officially Supported Splunk ...