Splunk Search

Getting first match field in a event

apand84
Engager

In a service log different API being invoked each API start with ~( like ~getenrolled, ~enroll, ~submit) so is there any way to extract first API being called by rex

i tried (~\w+){1} but it matching with all called API .

Tags (1)
0 Karma

woodcock
Esteemed Legend

Like this:

... | rex "~(?<FirstAPIName>\w+)"
0 Karma

apand84
Engager

Unfortunately it will match all called api (started with ~+API name )
I wanted to extract first matched api which is the main api call subsequent are sub api in service log .

0 Karma

woodcock
Esteemed Legend

Then this:

 ... | rex max_match=1 "~(?<FirstAPIName>\w+)"
0 Karma

niketn
Legend

Based on the sample data provided please try the following rex command with max_match=1 (which is by default):

|  makeresults
|  eval _raw="~getenrolled, ~enroll, ~submit, ~somethingelse, ~somethingnew"
|  rex  "~(?<FirstAPIName>[^,]+)," max_match=1

Alternatively, you can also use caret sign ( ^ ) if you want to extract required API Name only from beginning of raw data (depends on the data, if this does not work please provide sample raw data)

|  makeresults
|  eval _raw="~getenrolled, ~enroll, ~submit, ~somethingelse, ~somethingnew"
|  rex  "^~(?<FirstAPIName>[^,]+),"

PS: First two pipes with makeresults and eval are used to generate mock data for testing regular expression.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
Get Updates on the Splunk Community!

Detecting Remote Code Executions With the Splunk Threat Research Team

REGISTER NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If ...

Observability | Use Synthetic Monitoring for Website Metadata Verification

If you are on Splunk Observability Cloud, you may already have Synthetic Monitoringin your observability ...

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...