Splunk Search

Use other events fields as a field for your search

pmgahan
New Member

Hi,

I need to use events on a data source as a reference for other events

Example:

ID . | Name . |Type . | IDCategory
0|Category1|Category|null
1|Categoryt2|Category|null
3|Item1|Item|0
4|Item2|Item|1

What I need is to output the following for items 2 and 4:

3|Item1|Item|0|Category1
4|Item2|Item|1|Category2

Thank you!

Tags (1)
0 Karma

HiroshiSatoh
Champion

Try this!

(your search)
|eval Name=if(Type="Category","",Name)
|eval Category=if(Type="Category",Name,"")
|eval IDCategory=if(Type="Category",ID,IDCategory)
|eval ID=if(Type="Category","",ID)
|eval Type=if(Type="Category","",Type)
|stats earliest(ID) as ID,
            earliest(Name) as Name,
            earliest(Type) as Type,
            earliest(Category) as Category by IDCategory
0 Karma

pmgahan
New Member

This is adding a column and moving the Name to this new column but what I need is to add the Category name to the row where the categoryID matches the Category's row ID

Thank you!

0 Karma

HiroshiSatoh
Champion

The Joins command links events with common fields, but it is a very heavy search command.
Please link as much as possible with stats or transaction command.

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi pmgahan,
try something like this

index=your_index
| rename IDCategory AS ID_Key
| join ID_Key [ search index=your_index | rename IDCategory AS ID_Key Name AS Category_Name| fields ID_Key  Category_Name]
| table ID Name Type ID_Key Category_Name

Put attention that the search is the same in both main and sub search.
Bye.
Giuseppe

0 Karma

pmgahan
New Member

It doesn't seem to be working...
Take into account that from my expected results below, what I'm painting bold comes from rows 3 & 4 and what is italic comes from matching the IDCategory with rows 0 & 1 and adding the name as a new column.

ID . | Name . |Type . | IDCategory
0|Category1|Category|null
1|Categoryt2|Category|null
3|Item1|Item|0
4|Item2|Item|1

What I need is to output the following for items 2 and 4:

3|Item1|Item|0|Category1
4|Item2|Item|1|Category2

Thank you!

0 Karma

pmgahan
New Member

I figured that what I may have to do is the following

index=your_index
| rename IDCategory AS ID_Key
| join ID_Key [ search index=your_index | rename ID AS ID_Key Name AS Category_Name| fields ID_Key Category_Name]
| table ID Name Type ID_Key Category_Name

This should match the IDCategory of one row to the ID for the category and output the Name. It is still not working.

Thank you!

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi pmgahan,
in other words, you want only the rows where there's the match between IDCategory and ID, is it correct?
if I correctly understood, try something like this

index=your_index
| rename IDCategory AS ID_Key
| join ID_Key [ search index=your_index | rename IDCategory AS ID_Key Name AS Category_Name| fields ID_Key  Category_Name]
| search Category_Name=*
| table ID Name Type ID_Key Category_Name

In this way you'll have only

3|Item1|Item|0|Category1
4|Item2|Item|1|Category2

Bye.
Giuseppe

0 Karma

pmgahan
New Member

Thanks Giuseppe, but the result from your search would result as follows:

3|Item1|Item|0|Item1
4|Item2|Item|1|Item1

instead of

3|Item1|Item|0|Category1
4|Item2|Item|1|Category2

Patrick

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi pmgahan,
sorry I did an error in subsearch, try:

index=your_index 
| rename IDCategory AS ID_Key
| join ID_Key [ search index=your_index | rename ID AS ID_Key Name AS Category_Name | fields ID_Key  Category_Name ]
| search Category_Name=*
| table ID Name Type ID_Key Category_Name

it should run
after try

index=your_index IDCategory=*
| rename IDCategory AS ID_Key
| join ID_Key [ search index=your_index NOT IDCategory=* | rename ID AS ID_Key Name AS Category_Name| fields ID_Key  Category_Name]
| table ID Name Type ID_Key Category_Name

that should be quicker.

Bye.
Giuseppe

0 Karma
Get Updates on the Splunk Community!

.conf24 | Day 0

Hello Splunk Community! My name is Chris, and I'm based in Canberra, Australia's capital, and I travelled for ...

Enhance Security Visibility with Splunk Enterprise Security 7.1 through Threat ...

(view in My Videos)Struggling with alert fatigue, lack of context, and prioritization around security ...

Troubleshooting the OpenTelemetry Collector

  In this tech talk, you’ll learn how to troubleshoot the OpenTelemetry collector - from checking the ...