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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...