Splunk Search

Case with multiple matches

maniishpawar
Path Finder

Hello all,

I am trying this search but it's not working.
Only the first match count is returned.

index=abc* sourcetype=applogfile
| eval _rawtext=_raw
| eval P_ErrMsg=case(_rawtext LIKE "%conflict%", "FKEY1", _rawtext like "%FOREIGN KEY%", "FKEY",_rawtext like "%nonexistingvalue%","garbagevalue")
| stats count by P_ErrMsg

Tags (2)
0 Karma

DalJeanis
Legend

1) Case, in pretty much all languages, is equivalent to a nested if-then structure. You don't get multiple answers.

2) There is no reason to copy the data from _raw to _rawtext.

3) A simple rex will pull what you need, then you can change the values after the stats command.

index=abc* sourcetype=applogfile
| rex "(?<P_ErrMsg>conflict|FOREIGN KEY|nonexistingvalue)" max_match=0
| eval P_ErrMsg=mvdedup(P_ErrMsg)
| stats count by P_ErrMsg
| eval P_ErrMsg=case(P_ErrMsg=="conflict", "FKEY1",  
    P_ErrMsg=="FOREIGN KEY", "FKEY",
    P_ErrMsg=="nonexistingvalue","garbagevalue") 

niketn
Legend

@maniishpawar, can you please add some sample data where Only the first match count is returned?

| makeresults 
| eval _raw="some conflict while finding FOREIGN_KEY" 
| append 
    [| makeresults 
    | eval _raw="Event with nonexistingvalue"] 
| eval P_ErrMsg=case(searchmatch("conflict"), "FKEY1"
    ,searchmatch("FOREIGN KEY"), "FKEY"
    ,searchmatch("nonexistingvalue"),"garbagevalue") 
| stats count by P_ErrMsg

As @DalJeanis has mentioned you should avoid a command like | eval _rawtext=_raw to copy raw data over from one field to another. Alternative to Dal's approach, you can also try searchmatch() function which matches your criteria against the _raw data. Splunk Documentation for reference: http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/ConditionalFunctions#searchmatch....
PS: Pipes with | makeresults and | append are used to generate some mock data. You would need to replace with your base search. Also it is better id you added your own mocked up sample events (with sensitive information masked or anonymized)

| makeresults 
| eval _raw="some conflict while finding FOREIGN_KEY" 
| append 
    [| makeresults 
    | eval _raw="Event with nonexistingvalue"]  
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Best Practices: Splunk auto adjust pipeline queue

When you enable autoAdjustQueue in Splunk, maxSize should be understood as the queue size Splunk starts with ...

Laser Bananas and Edge Hubs: Exploring Operational Technology (OT) Data Through a ...

  OT is a different environment to traditional IT and can have interesting challenges when interfacing the ...

Event Series: Mastering AI Tokenomics and Splunk Agent Observability

Beyond the Black Box: Correlating AI Performance and Tokenomics with Splunk Agent Observability   As ...