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!

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...

Updated Team Landing Page in Splunk Observability

We’re making some changes to the team landing page in Splunk Observability, based on your feedback. The ...