Splunk Search

How to use Regex on non-uniform data

paulito
Explorer

I'm trying to extract a number that may not always be formatted the same way every time.

Examples:

 

 

OK: Process matching httpd is using 0% CPU
OK: Process matching httpd is using 1.1% CPU
OK: Process matching httpd is using 24.1% CPU

 

 

 

It's the "0%" that is tripping me up.

This will work for numbers with a decimal but not for a percentage that is just "0".

 

 

rex "using\s(?<CPU_util_perc>\d+.\d+)\%"

 

 

 

Any help is greatly appreciated.

Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Try this regex.  It accepts any combination of digits and decimals.

using\s(?<CPU_util_perc>[\d\.]+)\%

 You also could use

using\s(?<CPU_util_perc>.+)\%

since there's (probably) little chance of anything other than a number being in that position.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

somesoni2
Revered Legend

Give this a try

| rex "using\s(?<CPU_util_perc>\d+(\.\d+)*)\%"
OR
| rex "using\s(?<CPU_util_perc>[^\%]+)\%"
0 Karma

crv3347
Engager

This worked on the sample data you provided.

 

using\s(?<CPU_util_perc>[0-9\.]{1,4})%

 

richgalloway
SplunkTrust
SplunkTrust

Try this regex.  It accepts any combination of digits and decimals.

using\s(?<CPU_util_perc>[\d\.]+)\%

 You also could use

using\s(?<CPU_util_perc>.+)\%

since there's (probably) little chance of anything other than a number being in that position.

---
If this reply helps you, Karma would be appreciated.

paulito
Explorer

They both work!

 

Thank you

0 Karma

richgalloway
SplunkTrust
SplunkTrust

If your problem is resolved, then please click one of the "Accept as Solution" buttons to help future readers.

---
If this reply helps you, Karma would be appreciated.
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 ...