Splunk Search

How can I Rex to match until a string

mattodo
Explorer

Hi folks,

I'm new to regex and am struggling to extract a number from a field. I basically need the amount extracted from the following;

Date Name Amount Curr Type Status
------------------- -------------------- ---------- ---- ------------ ------
07.05.2016 22:58:09 Person's Name 5.00 GBP Sale Okay

It is the variable length text before the number that I am struggling with.

Please can you suggest a way in which I can extract just the 5.00 in the above example?

Thanks in advance 🙂

Matt

0 Karma
1 Solution

javiergn
Super Champion

Try this but ignoring the top 3 lines that I used to replicate your event.

Option 1 (from your raw event):

| stats count
| fields - count
| eval _raw = "07.05.2016 22:58:09 Person's Name 5.00 GBP Sale Okay"
| rex field=_raw "(?<Amount_num>\d+\.\d{2}) GBP"

Option 2 (assuming there's a field named Amount as indicated above):

| stats count
| fields - count
| eval Amount = "5.00 GBP"
| rex field=Amount "(?<Amount_num>\d+\.\d{2})"

View solution in original post

javiergn
Super Champion

Try this but ignoring the top 3 lines that I used to replicate your event.

Option 1 (from your raw event):

| stats count
| fields - count
| eval _raw = "07.05.2016 22:58:09 Person's Name 5.00 GBP Sale Okay"
| rex field=_raw "(?<Amount_num>\d+\.\d{2}) GBP"

Option 2 (assuming there's a field named Amount as indicated above):

| stats count
| fields - count
| eval Amount = "5.00 GBP"
| rex field=Amount "(?<Amount_num>\d+\.\d{2})"

mattodo
Explorer

Many thanks for the prompt reply Javiergn, It shows I am new to this because I copied and pasted from one of my records and the formatting was lost. There are actually many spaces between the field sections. I have now found the code sample feature 😉 so the data should look like this;

Date                Name                     Amount Curr Type         Status
------------------- -------------------- ---------- ---- ------------ ------
07.05.2016 22:58:09 Person's name                 5.00 GBP  Sale         Okay  

The rex you gave did not return anything. Is this due to the extra spaces?

Thanks again!

Matt

0 Karma

mattodo
Explorer

Hi again, your method worked great (minor error on my part) so I have exactly what I need.

Many thanks, you have really helped me out!

0 Karma

javiergn
Super Champion

Hi, no worries. Happy to help.

If you are happy with the response please mark it as answered so that others can benefit from it.

0 Karma

mattodo
Explorer

done 🙂 TY

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 ...