Splunk Enterprise

custom text to the results

selvam_sekar
Path Finder

Hi,

from the logs, i have extracted the below data(table1). I would like to add another column as in Table2 with custom keyword if filename begins xyz then "Core".

Please could you suggest what splunk query or logic we could apply?

Splunk Query:

base search | rex field User | rex field Folder | rex filed File
| table User  Folder File

Table1: 

User Folder File
ABC first xyz07122023

 

Table 2: Required Output

User Folder File Consumer
ABC first xyz07122023 Core
Labels (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Use the eval command to add a field ("column").  The match function will compare a field to a string/regular expression.

base search 
| rex field User | rex field Folder | rex field File
| eval Consumer = if(match(File, "^xyz"), "Core", "")
| table User Folder File Consumer

 

---
If this reply helps you, Karma would be appreciated.

View solution in original post

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Use the eval command to add a field ("column").  The match function will compare a field to a string/regular expression.

base search 
| rex field User | rex field Folder | rex field File
| eval Consumer = if(match(File, "^xyz"), "Core", "")
| table User Folder File Consumer

 

---
If this reply helps you, Karma would be appreciated.
0 Karma

selvam_sekar
Path Finder

Thanks, it worked as expected.

If we have 3 or multiple matches, do we required to write individual eval as mentioned below?

for example,

File Consumer
XYZ_*Core
ABC_*Core
MNP_*Non-Core
RSTNon-Core

 

SPL: 

 

base search 
| rex field User | rex field Folder | rex field File
| eval Consumer = if(match(File, "^xyz"), "Core", "")
| eval Consumer = if(match(File,"^ABC"),"Core","")
| eval Consumer = if(match(File,"^MNP"),"Non-Core","")
| table File Consumer

 

 

0 Karma

richgalloway
SplunkTrust
SplunkTrust

That won't work as expected.  If File is "XYZ_*" then Consumer will be set to "" because of the last eval statement.  For instances like this, use the case function.

 

base search 
| rex field User | rex field Folder | rex field File
| eval Consumer = case(match(File, "^xyz"), "Core",
                       match(File,"^ABC"),"Core",
                       match(File,"^MNP"),"Non-Core",
                       1==1,"Others")
| table File Consumer

 

 

---
If this reply helps you, Karma would be appreciated.
0 Karma

selvam_sekar
Path Finder

Thanks a lot

0 Karma

selvam_sekar
Path Finder

Excellent! Many thanks.

is it possible if we make the non-matching of  the below files to map "Others".

for example,

other than file name starts with xyz, ABC, MNP  as Others.

0 Karma

richgalloway
SplunkTrust
SplunkTrust

See my updated reply.  The last clause of this case function handles non-matches.

---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...

Get the T-shirt to Prove You Survived Splunk University Bootcamp

As if Splunk University, in Las Vegas, in-person, with three days of bootcamps and labs weren’t enough, now ...

Wondering How to Build Resiliency in the Cloud?

IT leaders are choosing Splunk Cloud as an ideal cloud transformation platform to drive business resilience,  ...