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!

Splunk Forwarders and Forced Time Based Load Balancing

Splunk customers use universal forwarders to collect and send data to Splunk. A universal forwarder can send ...

NEW! Log Views in Splunk Observability Dashboards Gives Context From a Single Page

Today, Splunk Observability releases log views, a new feature for users to add their logs data from Splunk Log ...

Last Chance to Submit Your Paper For BSides Splunk - Deadline is August 12th!

Hello everyone! Don't wait to submit - The deadline is August 12th! We have truly missed the community so ...