Splunk Search

How do I get results in search A that are not in search B?

annoyedmildly
Engager

I want to find entries added to a sourcetype today, that haven't been seen in the last N days. I've tried search A | fields + want | fields - ignore | search NOT [search B | fields + want | fields - ignore] and that doesn't seem to work. I still see results that are in search B.

Tags (3)
1 Solution

sideview
SplunkTrust
SplunkTrust

Note: fields want | fields - ignore is redundant. Because if you narrow the field list to just the want field, then there is no ignore field to remove.

Also, when you're using subsearches it can be helpful to look at the search inspector "Actions > Inspect Search". You'll see that just putting NOT in front of a subsearch will not NOT the entire subsearch set. I think it will only NOT the first parenthetical term. Possibly wrapping the entire subsearch in parens will do it. Otherwise you'll have to play around with the format command if you want to take this approach.

A subsearch might be the way to go. However for fun here's an approach that doesn't use subsearches. This shows URL's in access data that were accessed previously in the time range, but not accessed today.

sourcetype=access_combined | eval happened_today=if(relative_time(now(),"@d")<=_time,1,0) | eval happened_before=if(relative_time(now(),"@d")>_time,1,0) | streamstats sum(happened_today) as happened_today sum(happened_before) as happened_before by url | stats count by url happened_today happened_before | search happened_before>0 happened_today=0

View solution in original post

sideview
SplunkTrust
SplunkTrust

Note: fields want | fields - ignore is redundant. Because if you narrow the field list to just the want field, then there is no ignore field to remove.

Also, when you're using subsearches it can be helpful to look at the search inspector "Actions > Inspect Search". You'll see that just putting NOT in front of a subsearch will not NOT the entire subsearch set. I think it will only NOT the first parenthetical term. Possibly wrapping the entire subsearch in parens will do it. Otherwise you'll have to play around with the format command if you want to take this approach.

A subsearch might be the way to go. However for fun here's an approach that doesn't use subsearches. This shows URL's in access data that were accessed previously in the time range, but not accessed today.

sourcetype=access_combined | eval happened_today=if(relative_time(now(),"@d")<=_time,1,0) | eval happened_before=if(relative_time(now(),"@d")>_time,1,0) | streamstats sum(happened_today) as happened_today sum(happened_before) as happened_before by url | stats count by url happened_today happened_before | search happened_before>0 happened_today=0

sideview
SplunkTrust
SplunkTrust

Ah got it. yea "fields foo" will leave all the fields that begin with underscores untouched. that's why you often see "fields foo | fields - _*"

0 Karma

annoyedmildly
Engager

fields - _raw otherwise I get the raw record in the output when I just want the values for the field of interest. However, using stats this isn't an issue.

0 Karma
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...