Splunk Search

Extract a number before a string

Vicky84
Explorer

Completely New to regex, I have a log as below and wanted to extract the percentage i.e. "28" and then check it with threshold limit.

10A 4.5A 4.2A 28% /text/path

0 Karma

MuS
Legend

Hi Vicky84,

you can use this regex to get it:

 your base search here | rex "(?:[^\s]+\s){3}(?<perc>\d+)%" | ...

or this one:

your base search here | rex "\S+\s\S+\s\S+\s(?<perc>\d+)%" | ...

The first regex matches three times everything that is not a whitespace followed by a whitespace and take all digits in to the field called perc.
the second regex matches any non-whitespace character followed by one space followed by any non-whitespace character followed by one space any non-whitespace character followed by one space and take all digits in to the field called perc.

Hope this helps and checkout the web page regex101.com which provides good examples and helps to learn regex 😉

cheers, MuS

0 Karma

khaynes_splunk
Splunk Employee
Splunk Employee

If you use the Field Extraction tool built into Splunk, you will not need to build the regex yourself. When viewing such an event in Splunk, open the event by clicking the ">" carat to the left of the event, then click the Event Actions button, then click Extract Fields. This will take you to the Extract Fields wizard where you just highlight the text you want to extract and give the field a name...

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...