Splunk Search

How to use NOT in an IF condition?

prakashbhanu407
New Member

I have 2 files: Account and Account.TXT. I have to get only the "Account" file details. I tried:

if(  (like(filename,"Account%") AND NOT like(filename,"Account%.txt%")  ),filename,"X")

but it is returning both file types.

What is the mistake here?

0 Karma

woodcock
Esteemed Legend

Like this:

... | eval test = if((like(filename, "Account") AND NOT like(filename, "Account%.txt")), filename," X")

Your problem is the wildcard character %, most of which you do not need.

0 Karma

sundareshr
Legend

Why not just use .. | if(filename="Account", x, y) and skip the wildcard altogether?

prakashbhanu407
New Member

actually i have 2 sets of files X and Y,
X has about 10 different types of files including "AccountyyyyMMdd.hhmmss"(no extension)
Y has another 8 files types including "AccountyyyyMMdd.hhmmss.TXT"

So for the "X" type of files I have multiple "like()" functions in the if() condition, it should only retrieve data for "Account" file but it is also picking up for "Account.TXT" which should be of type "Y"
like below
if(
(like() like () ..... (like(filename,"Account%") AND NOT like(filename,"Account%.txt%")) ),"X" ,
if( (like() like()...like(filename,"Account%.txt%"),"Y","Other")
)
)

0 Karma

sundareshr
Legend

Try the match() with regex . Something like this should work

.... | eval x=case(match(filename, "Account\d+\.\d+$", "no extn", filename="\.(txt|TXT)$", "with extn", 1=1, "no match")
0 Karma
Get Updates on the Splunk Community!

Enter the Dashboard Challenge and Watch the .conf24 Global Broadcast!

The Splunk Community Dashboard Challenge is still happening, and it's not too late to enter for the week of ...

Join Us at the Builder Bar at .conf24 – Empowering Innovation and Collaboration

What is the Builder Bar? The Builder Bar is more than just a place; it's a hub of creativity, collaboration, ...

Combine Multiline Logs into a Single Event with SOCK - a Guide for Advanced Users

This article is the continuation of the “Combine multiline logs into a single event with SOCK - a step-by-step ...