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!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...