Splunk Search

Get the difference of 2 columns after Grouping

GadgetGeek
Path Finder

After grouping to display a chart, where there are only 2 values produced, how do I calculate the difference between the 2 columns?

e.g. Sample search :

<search terms> | rex "Sent\s+(?<ProductType>[a-zA-Z]*)" | rex "Product:\s+(?<ProductId>[a-zA-Z0-9]{5}-[a-zA-Z0-9]{8})" | eval ProductType=if(ProductType="Apple", "Fruit", "Veg")  | chart count(ProductType) as Total by ProductId, ProductType

Giving:

ProductId                Fruit      Veg
12345-12345678            3          2

Where I want:

ProductId                Fruit      Veg  Diff
12345-12345678            3          2    1
Tags (1)
0 Karma
1 Solution

somesoni2
Revered Legend

If the ProductType is static, then the field name after chart will also be fixed, you can just use an eval to get the difference.

<search terms> | rex "Sent\s+(?<ProductType>[a-zA-Z]*)" | rex "Product:\s+(?<ProductId>[a-zA-Z0-9]{5}-[a-zA-Z0-9]{8})" | eval ProductType=if(ProductType="Apple", "Fruit", "Veg")  | chart count(ProductType) as Total by ProductId, ProductType | eval Diff=abs(Fruit-Veg)

View solution in original post

0 Karma

somesoni2
Revered Legend

If the ProductType is static, then the field name after chart will also be fixed, you can just use an eval to get the difference.

<search terms> | rex "Sent\s+(?<ProductType>[a-zA-Z]*)" | rex "Product:\s+(?<ProductId>[a-zA-Z0-9]{5}-[a-zA-Z0-9]{8})" | eval ProductType=if(ProductType="Apple", "Fruit", "Veg")  | chart count(ProductType) as Total by ProductId, ProductType | eval Diff=abs(Fruit-Veg)
0 Karma

GadgetGeek
Path Finder

Many thanks!!

0 Karma

GadgetGeek
Path Finder

It works - how does it work though, as "Fruit" and "Veg" are string values for 'ProductType'? It's kind of like:

Diff=abs(ProductType="Fruit", ProductType="Veg)..

0 Karma

somesoni2
Revered Legend

After your chart command, the values of Product Type becomes field names. So you'll have actual field names like 'Fruit' and 'Veg', that what's been used for calculation.

0 Karma
Get Updates on the Splunk Community!

Automatic Discovery Part 1: What is Automatic Discovery in Splunk Observability Cloud ...

If you’ve ever deployed a new database cluster, spun up a caching layer, or added a load balancer, you know it ...

Real-Time Fraud Detection: How Splunk Dashboards Protect Financial Institutions

Financial fraud isn't slowing down. If anything, it's getting more sophisticated. Account takeovers, credit ...

Splunk + ThousandEyes: Correlate frontend, app, and network data to troubleshoot ...

 Are you tired of troubleshooting delays caused by siloed frontend, application, and network data? We've got a ...