Splunk Search

Extract from Multiple Fields and Consolidate using Stats Count

sridharadurthi
Engager

Hi Team,

I have two different fields (Ex. A and B). Value A will come for some results and B will come for some. While I am using below query, it is only pulling A or B.

index="XYZ"  (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200
| stats count by A StatusCode - only A events are getting displayed

index="XYZ"  (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200
| stats count by B StatusCode - only B events are getting displayed

index="XYZ"  (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200
| stats count by A B StatusCode - it is not displaying any table

 

How to display both A and B colums combined and have the status code as well in the table?

Labels (3)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

OK try it with double quotes on the stats command (which is counter-intuitive!)

index="apigee" (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200 | eval "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode" = coalesce('BackendResponse.content.reasonCode', 'ConsumerResponse.content.reasonCode') | stats count by "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode"

View solution in original post

0 Karma

yuanliu
SplunkTrust
SplunkTrust

You need to first think through what the problem you are trying to solve by using sample data.  Let's say the search index="XYZ"  (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200 returns the following results.

StatusCodeAB
200some A value 
200some other A value 
200 Some B value
200 Some other B value
200Even more A value 

Can you show the result table that you are looking for?


How to display both A and B colums combined and have the status code as well in the table?


If A and B do not exist in the same event, I see no meaningful way to display both of them.

0 Karma

sridharadurthi
Engager

Thanks for swift reply @yuanliu . Here is the sample table I am looking for

StatusCodeA or BCount
200some A/B value2
200some A/B value1
200some A/B value10
200some A/B value8
200some A/B value5


A & B coulmns should come together as one and based on their values it should add to the count. More detailed way

StatusCodeA or BCount
200Upgrade2
200Downgrade1
200Retain10
200Cancel8
200New Customer5

 

0 Karma

yuanliu
SplunkTrust
SplunkTrust
A & B coulmns should come together as one and based on their values it should add to the count.

This is clearer than the original description.  If A and B should come together, they have to be combined before groupby, and used as a single groupby.  The reason why your first attempt did produce results is because as two separate groupby terms, both must exist in the same events.

The solution will depend on whether A and B are mutually exclusive.  From your original result, it seems that they are exclusive.  So,

 

index="XYZ"  (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200
| eval "A OR B" = coalesce(A, B)
| stats count by "A OR B" StatusCode

 

Tags (1)
0 Karma

sridharadurthi
Engager

When I am trying your query, it is showing that number events on the top. But not displayin the results in the statistics. Like below

sridharadurthi_0-1720474272458.png

 

0 Karma

yuanliu
SplunkTrust
SplunkTrust

This only means that your data set is not as you described.  Alternatively, there was some mistake in your search as @ITWhisperer speculated.  For example, maybe you misspelled A or B (these are not real field names I am certain).

I can run my code with this mock dataset:

ABStatusCode
Upgrade 200
 Downgrade200
 Upgrade200
Retain 200
 Cancel200
Cancel 200
 Cancel200
Cancel 200
 Cancel200
Cancel 200
 Cancel200
Cancel 200
 Retain200
Retain 200
Retain 200
 Retain200
Retain 200
Retain 200
 Retain200
Retain 200

If you manually count, this dataset should give your mock result and it does.  Here is full emulation that you can run an compare with real data:

 

| makeresults format=csv data="StatusCode, A, B
200, Upgrade,
200, , Downgrade
200, , Upgrade
200, Retain,
200, , Cancel
200,Cancel,
200, , Cancel
200,Cancel,
200, , Cancel
200,Cancel,
200, , Cancel
200,Cancel,
200, , Retain
200, Retain,
200, Retain,
200, , Retain
200, Retain,
200, Retain,
200, , Retain
200, Retain,
200, Retain,
200, New Customer,
200, , New Customer
200, , New Customer
200, New Customer,
200, , New Customer"
``` the above emulates
index="XYZ"  (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200
```
| eval "A OR B" = coalesce(A, B)
| stats count by "A OR B" StatusCode

 

The output is exactly like your mock result:

A OR BStatusCodecount
Cancel2008
Downgrade2001
New Customer2005
Retain20010
Upgrade2002
0 Karma

sridharadurthi
Engager

@yuanliu this is the query which I am using to filter the data

index="apigee" (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200 | eval "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode" = coalesce(BackendResponse.content.reasonCode, ConsumerResponse.content.reasonCode) | stats count by "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode" StatusCode

It is showing the event count, but it is not generating the results. Highlited the same.

sridharadurthi_0-1720532706875.png

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

When field names have special characters in, they often need single quotes around them (double if they are on the left of the assignment). Try this

| eval "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode" = coalesce('BackendResponse.content.reasonCode', 'ConsumerResponse.content.reasonCode')
| stats count by 'BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode' StatusCode
0 Karma

sridharadurthi
Engager

@ITWhisperer , I tried but no luck 😞 . It is displaying the count but not displaying the stats

sridharadurthi_1-1720539436378.png

 

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try without StatusCode on the stats

index="apigee" (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200 | eval "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode" = coalesce('BackendResponse.content.reasonCode', 'ConsumerResponse.content.reasonCode') | stats count by 'BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode'
0 Karma

sridharadurthi
Engager

No luck 😞 

sridharadurthi_0-1720542382140.png

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Please try it exactly as I showed you - I have already explained that you need double quotes to the left of the assignment and single quotes to the right - if you do not follow simple instructions like this, you will struggle to get a working solution!

0 Karma

sridharadurthi
Engager

@ITWhispererthanks for being patient with me. I have copied the same query which you have mentioned and this is the result

sridharadurthi_0-1720543269927.png

index="apigee" sourcetype!="apigee:nginx" (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200 | eval "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode" = coalesce('BackendResponse.content.reasonCode', 'ConsumerResponse.content.reasonCode')
| stats count by 'BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode'

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

OK try it with double quotes on the stats command (which is counter-intuitive!)

index="apigee" (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200 | eval "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode" = coalesce('BackendResponse.content.reasonCode', 'ConsumerResponse.content.reasonCode') | stats count by "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode"
0 Karma

sridharadurthi
Engager

Finally it is working now 🙂 Thank you so much. You made my day.

sridharadurthi_0-1720544170590.png

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Please share the search which is giving this result.

0 Karma

sridharadurthi
Engager

this is the query which I am using to filter the data

index="apigee" (ProxyPath="/xyz" OR ProxyPath="/abc") AND StatusCode=200 | eval "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode" = coalesce(BackendResponse.content.reasonCode, ConsumerResponse.content.reasonCode)
| stats count by "BackendResponse.content.reasonCode OR ConsumerResponse.content.reasonCode" StatusCode

 

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!

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...

Customer success is front and center at .conf25

Hi Splunkers, If you are not able to be at .conf25 in person, you can still learn about all the latest news ...