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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...