Splunk Search

Why is statement not returning expected results when counting events by "UserAgent" field?

SplunkySplunk
Explorer

Hello
I'm trying to count events by field called "UserAgent"
If im searching for the events without any calculated field im getting results from different UserAgents
But once im using eval, I don't get the expected results
For example:
I've tried this eval and im getting only "android" also im searching for "ios" only with 

 

"ContextData.UserAgent"=*ios*

 

as part of my query 

 

| eval UserAgent = if("ContextData.UserAgent"="*ios*","ios","android")

 

 what im doing wrong ?

Labels (2)
Tags (2)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

A couple of things wrong - field names should be in single quotes not double quotes when on the right hand side of the evaluation - equalities don't work with *, that's just for search filters, try match()

| eval UserAgent = if(match('ContextData.UserAgent',"ios"),"ios","android")
0 Karma

sarit_s
Communicator

Thanks !

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @SplunkySplunk,

the issue is that sometimes, having special chars (as dot) in the field names the eval command fails, use rename and it will work:

| rename ContextData.UserAgent AS ContextData_UserAgent
| eval UserAgent = if("ContextData_UserAgent"="*ios*","ios","android")

Ciao.

Giuseppe

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!

Level Up Your .conf25: Splunk Arcade Comes to Boston

With .conf25 right around the corner in Boston, there’s a lot to look forward to — inspiring keynotes, ...

Manual Instrumentation with Splunk Observability Cloud: How to Instrument Frontend ...

Although it might seem daunting, as we’ve seen in this series, manual instrumentation can be straightforward ...

Take Action Automatically on Splunk Alerts with Red Hat Ansible Automation Platform

Ready to make your IT operations smarter and more efficient? Discover how to automate Splunk alerts with Red ...