Splunk Search

How to achieve conditional formatting of values with eval if and regex?

erikschubert
Engager

Hello everyone,

I have the following field and example value: sourcePort=514.000

I'd like to format these fields in such a way, that only the first digits until the point are kept. Furthermore, this should only apply to a certain group of events (group one). 
Basically: 
before: sourcePort=514.000
after:    sourcePort=514


What I have until now:
search...
| eval sourcePort=if(group=one, regex part, sourcePort)

The regex to match only the digits is  ^\d{1,5}
However, I am unsure how to work with the regex and if it is even possible to achieve my goal using this.


Thanks in advance

Labels (4)
0 Karma

tpickle
Splunk Employee
Splunk Employee

I would use split and mvindex instead of rex:

| eval sourcePort=if(group=one,mvindex(split(sourcePort,"."),0),sourcePort)
0 Karma

diogofgm
SplunkTrust
SplunkTrust

You can use | rex to achieve that:

|rex field=sourcePort "(?<src_port>\d{1,5})"
|eval sourcePort=if(group=one,src_port,sourcePort)

 

------------
Hope I was able to help you. If so, some karma would be appreciated.
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

You can use the replace function (which supports regex)

| eval sourcePort=if(group=one,replace(sourcePort,"(?<p>\d{1,5})\.(?<q>.*)","\1"),sourcePort)
0 Karma
Get Updates on the Splunk Community!

Leveraging Detections from the Splunk Threat Research Team & Cisco Talos

  Now On Demand  Stay ahead of today’s evolving threats with the combined power of the Splunk Threat Research ...

New in Splunk Observability Cloud: Automated Archiving for Unused Metrics

Automated Archival is a new capability within Metrics Management; which is a robust usage & cost optimization ...

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...