Splunk Search

using rex to extract from json

gds506
New Member

Hi,
I'm working on a akamai json and I want to extract the OS name from the message.UA field. Basically, if you look at the fake sample string below, I only want to get Windows (the part of the string between the first ( and the next %:

Mozilla%2f5.0%20(Windows%20NT%2018.0%3b%20Win64%3b%20x64)%20AppleWebKit%2f580.36%20(KHTML,%20like%20Gecko)%20Chrome%2f81.0.4042.140%20Safari%2f537.36

I already created a regular expression that will do exactly what I want but I'm not able to make it work with rex (As you can imagine I'm new in Splunk). Here is how I'm trying to use it.

index=akamai | regex field = message.UA "(?<=\()(.*?)(?=\%)"| top message.UA

When I run it I get a

Error in 'rex' command: The regex
'(?<=()(?=\%)' does not extract
anything. It should specify at least
one named group. Format: (?...).

Any idea of how to accomplish this extraction?

Thanks!

0 Karma

darrenfuller
Contributor

Hi GDS...

you asked about REX, but in the example given you have used regex (but then used the syntax for rex... so i am going to answer with rex..

Here is a run anywhere example that answers (i think) your requirements.

| makeresults | fields - _time
| eval message.UA="Mozilla%2f5.0%20(Windows%20NT%2018.0%3b%20Win64%3b%20x64)%20AppleWebKit%2f580.36%20(KHTML,%20like%20Gecko)%20Chrome%2f81.0.4042.140%20Safari%2f537.36"
| rex field=message.UA "^[^\(]+\((?<OS>[^\%]+)\%"

in your regex... you don't have a named capture group: ie... (?PATTERN) ... that's what the system is complaining about in the error stated above

0 Karma
Get Updates on the Splunk Community!

This Week's Community Digest - Splunk Community Happenings [9.26.22]

Get the latest news and updates from the Splunk Community here! Upcoming User Group Events! &#x1f44f; Check ...

BSides Splunk 2022 - The Call for Papers is now Open!

TLDR; Main Site: https://bsidessplunk.com CFP Site: https://bsidessplunk.com/cfp CFP Opens: December 15th, ...

Sending Metrics to Splunk Enterprise With the OpenTelemetry Collector

This blog post is part of an ongoing series on OpenTelemetry. The OpenTelemetry project is the second largest ...