Splunk Search

extra data from particular filed and show in chart

dyapasrikanth
Path Finder

I have logs like 

{"message": "Submitted amount category1: 213, category2: 543.56, category3: 4343.00", "specialCustomer": "true"}

I am trying to extract sum of amount by each category and customerType. My query is like this

 

 

| search message="Submitted amount *"
| rex field=message "(?<category>\w+): (?<amount>\d*\.?\d*)"
| eval userType=if(isnotnull(specialCustomer), "Special", "Normal")
| chart sum(amount) as Amount by userType, category
| addtotals
| eval category=case(
category="category1", "Category 1", 
category="category2", "Category 2",
category="category3", "Category 3")

 

 

However I am getting only one category data, what is wrong with my regex? do I need to use extract for this?

Labels (3)
0 Karma
1 Solution

scelikok
SplunkTrust
SplunkTrust

Hi @dyapasrikanth,

rex command stops on the first match as default. You should use max_match option;

| search message="Submitted amount *" 
| rex field=message max_match=0 "(?<category>\w+): (?<amount>\d*\.?\d*)" 
| eval userType=if(isnotnull(specialCustomer), "Special", "Normal") 
| chart sum(amount) as Amount by userType, category 
| addtotals 
| eval category=case(
    category="category1", "Category 1", 
    category="category2", "Category 2",
    category="category3", "Category 3")
If this reply helps you an upvote and "Accept as Solution" is appreciated.

View solution in original post

0 Karma

scelikok
SplunkTrust
SplunkTrust

Hi @dyapasrikanth,

rex command stops on the first match as default. You should use max_match option;

| search message="Submitted amount *" 
| rex field=message max_match=0 "(?<category>\w+): (?<amount>\d*\.?\d*)" 
| eval userType=if(isnotnull(specialCustomer), "Special", "Normal") 
| chart sum(amount) as Amount by userType, category 
| addtotals 
| eval category=case(
    category="category1", "Category 1", 
    category="category2", "Category 2",
    category="category3", "Category 3")
If this reply helps you an upvote and "Accept as Solution" is appreciated.
0 Karma

dyapasrikanth
Path Finder

I got it finally with mvzip & mvexpand, not sure it is a best solution

 

| rex field=message max_match=0 "(?<category>\w+):\s(?<amount>\d*\.?\d*)"
| eval temp = mvzip(category,amount, "#")
| mvexpand temp
| rex field=temp "(?<category>.+)#(?<amount>.+)"
| eval category=case(
    category="category1", "Category 1", 
    category="category2", "Category 2",
    category="category3", "Category 3")
| chart sum(amount) as Amount by userType, category
| addtotals

 

  

 
0 Karma

dyapasrikanth
Path Finder

Thanks for your quick reply, yes that max_match=0 did the trick. But why my sum is going wrong ? 
For all categories it is giving first category amount only.

If I table it, they are coming as group instead in each row.

Tags (1)
0 Karma
Get Updates on the Splunk Community!

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

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

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...