Splunk Search

How do I develop a rex command that extracts non-contiguous characters into one capturing group?

pc1234
Explorer

I need some assistance coding a rex statement to extract data from events generated by a Powershell script.
Sample data:

Name                                   Port        Description             Protocol
Windows remote mgmt                 RPC 135     @FirewallAPI              TCP
Corenet-12345                         421                                    TCP
Port 75 and 443                                                              UDP

I have two questions:
1) How do I extract non-contiguous characters into one capturing group (one for name and the other for port)
2) How do I handle cases where one or more fields are blank? (in this case, the port in the second row and both the port and description in the third row?

Thanks in advance for your help.

0 Karma

lakromani
Builder

If data are at fixed length, you can do some like this:

(?<name>.{39})(?<port>.{15})(?<info>.{26})(?<protocol>.+)

Look at this:
https://regex101.com/r/RwdS6Y/2

0 Karma

davebrooking
Contributor

Hi

Does your data really look like that, with the fields always fixed length as you have shown, or can the fields be variable length?

Dave

0 Karma

bcdady
Explorer

What options do you have to modify the generating PowerShell cmdlet or function?
We might be able to avoid having to use RegEx / rex altogether. Since the various, .Name, .Port, .Description and .Protocol are all properties of the object(s) returned by the calling PowerShell, you could consider formatting the output as CSV, XML or at least another delimited output that would be easier to parse with rex.

0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...