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!

[Puzzles] Solve, Learn, Repeat: Dynamic formatting from XML events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  &#x1f680; Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

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